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

时间:2017-06-07 07:25:09

标签: php cron sftp

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

使用date()

#!/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')) 一起使用,所以

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath( 'obj/Cube/' ); 
mtlLoader.load( 'cube.mtl', function( materials ) {
materials.preload();

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials( materials );
    objLoader.setPath( 'obj/Cube/' );
    objLoader.load( 'cube.obj', function( object ) {
        object.traverse( function( child ) { 
            if ( child instanceof THREE.Mesh ) { 
                child.castShadow = true;
                child.material.color.set( 0x00ff00 );  // change color without dat.GUI
            } 
        } );
    companion = object;
    scene.add( object );
    });
});

gui = new dat.GUI();

parameters = 
{   
    color: "#ff0000",
    visible: true,
};

var objColor = gui.addColor( parameters, 'color' ).name('Color (Diffuse)').listen();
objColor.onChange(function(value) // onFinishChange
{   companion.material.color.setHex( value.replace("#", "0x") );   });

gui.open();
updateColor(companion);