PHP:一个人如何自动化脚本每天更改日期


#!/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服务器下载备份,但我每天都会在脚本中手动更改日期( bold (。问题:有没有办法使脚本自动更改日期,以便我可以设置Cron作业?

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

替换您的日期
date('d-M-Y')

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

这将需要服务器当前时间。

最新更新