Php: 如何使脚本自动化以每天更改日期?

Php: How can one automate a script to change dates everyday?

#!/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 作业?

将您的日期替换为

date('d-M-Y')

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

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

使用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'))