【问题标题】:Php: How can one automate a script to change dates everyday? [duplicate]Php:如何自动化脚本以每天更改日期? [复制]
【发布时间】:2017-06-07 07:25:09
【问题描述】:
#!/usr/bin/php

<?php
$username = "user";
$password = "pass";
$url      = '10.168.8.666';
// Make our connection
$connection = ssh2_connect($url);

// Authenticate
if (!ssh2_auth_password($connection, $username, $password))
     {echo('Unable to connect.');}

// Create our SFTP resource
if (!$sftp = ssh2_sftp($connection))
     {echo ('Unable to create SFTP connection.');}


$localDir  = 'file:///home/batman/Downloads/dbs';
$remoteDir = '/home/batbackup/Dropbox/dbs';
// download all the files
$files = scandir ('ssh2.sftp://' . $sftp . $remoteDir);
if (!empty($files)) {
  foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        if (substr($file, 0, 11)=='07-Jun-2017'){
            # code...
              ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");

        }

    }
  }
}
?>

我每天都使用此脚本从 sftp 服务器下载备份,但我必须每天手动更改脚本中的日期(粗体)。 问题:有没有办法让脚本自动更改日期,以便我可以设置一个 cron 作业?

【问题讨论】:

    标签: php cron sftp


    【解决方案1】:

    使用date()

    date('d-M-Y')
    

    会变成

    #!/usr/bin/php
    
    <?php
    $username = "user";
    $password = "pass";
    $url      = '10.168.8.666';
    // Make our connection
    $connection = ssh2_connect($url);
    
    // Authenticate
    if (!ssh2_auth_password($connection, $username, $password))
         {echo('Unable to connect.');}
    
    // Create our SFTP resource
    if (!$sftp = ssh2_sftp($connection))
         {echo ('Unable to create SFTP connection.');}
    
    
    $localDir  = 'file:///home/batman/Downloads/dbs';
    $remoteDir = '/home/batbackup/Dropbox/dbs';
    // download all the files
    $files = scandir ('ssh2.sftp://' . $sftp . $remoteDir);
    if (!empty($files)) {
      foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
    
            if (substr($file, 0, 11) == date('d-M-Y')) {
                # code...
                  ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");
    
            }
    
        }
      }
    }
    ?>
    

    如果您希望它一直存在,比如说昨天,您可以将它与strtotime() 一起使用,所以

    date('d-M-Y', strtotime('yesterday'))
    

    【讨论】:

    • 不要回答重复的问题,而是将它们投票为重复。
    • 对不起,你是对的
    • 感谢百万伴侣!!
    【解决方案2】:

    将您的日期替换为

    date('d-M-Y')
    

    http://php.net/manual/fr/function.date.php

    这将采用服务器当前时间。

    【讨论】:

    • 不要回答重复的问题,而是将它们投票为重复。
    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 2018-07-20
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多