Php 比较两个unix时间文件以删除旧文件
我正在使用下面的代码获取我的云服务器上文件的Php 比较两个unix时间文件以删除旧文件,php,datetime,timestamp,unix-timestamp,Php,Datetime,Timestamp,Unix Timestamp,我正在使用下面的代码获取我的云服务器上文件的时间戳,然后删除任何超过一定年龄的文件 目前,代码被设置为删除任何超过10分钟的内容以进行测试 但是它会删除所有内容,即使我上传了一些文件,然后在10秒后运行脚本 有什么小问题我没有看到吗 $auth->authenticate(); //connect $conn = new CF_Connection($auth); //create a handle for the CLOUD FILES contain
时间戳
,然后删除任何超过一定年龄的文件
目前,代码被设置为删除任何超过10分钟的内容以进行测试
但是它会删除所有内容,即使我上传了一些文件,然后在10秒后运行脚本
有什么小问题我没有看到吗
$auth->authenticate();
//connect
$conn = new CF_Connection($auth);
//create a handle for the CLOUD FILES container we want to delete from
$daily_dose = $conn->get_container('daily_dose');
//delete the obeject
$daily_packages = $daily_dose->list_objects();
//print_r($daily_packages);
//$public_container = $conn->get_container("public");
//$file_age = 86400; // 1 day in seconds
$file_age = 600; // 10 minutes
echo 'current time: ';
echo $current_time = time();
echo '<br>';
echo 'time offset: ';
echo $previous_day = $current_time - $file_age;
echo '<br>';
foreach($daily_packages as $package)
{
echo $item = $daily_dose->get_object($package);
$modified = $item->last_modified;
echo ' ' . $modified;
echo ' -> ' . strtotime($modified);
echo '<br>';
//If the modified time of the file is less the current time - 1 day in seconds (older than 1 day) delete it
if ( $modified < $previous_day )
{
//delete the file
$daily_dose->delete_object($item);
}
}
$auth->authenticate();
//连接
$conn=新的CF\U连接($auth);
//为要从中删除的云文件容器创建句柄
$daily_dose=$conn->获取_容器(“daily_dose”);
//删除对象
$daily_packages=$daily_dose->list_objects();
//打印(每日包裹);
//$public_container=$conn->get_container(“public”);
//$file_age=86400;//以秒计1天
$file_age=600;//10分钟
回显“当前时间:”;
echo$current_time=time();
回声“
”;
回波“时间偏移:”;
echo$previous\u day=$current\u time-$file\u age;
回声“
”;
foreach(每日套餐为$package)
{
echo$item=$daily\u dose->get\u object($package);
$modified=$item->last\u modified;
回显“”。$已修改;
echo'->'.strotime($modified);
回声“
”;
//如果文件的修改时间小于当前时间-1天(以秒为单位)(大于1天),请将其删除
如果($修改<$前一天)
{
//删除该文件
$daily_dose->删除_对象($item);
}
}
如果您在$modified上调用strotime(),我想它已经不是UNIX时间戳了。
time()确实返回UNIX时间戳
那么,支票不应该是这样的吗
if(strtotime($modified) < $previous_day)
if(strotime($modified)<$previous\u day)
通过这种方式,您可以比较UNIX时间戳,并且如果$item->last\u modified返回正确的值,那么代码应该可以工作
如果没有,请确保time()返回的值与软件在$items上设置上次修改值(易于通过一些调试输出进行检查)之间没有时间或时区差异。您还可以包括
echo
'd,这对我们也很有用。$item->last\u modified
中到底有什么内容?实际上,它是不是与time()
返回的时间相同的时间戳?假设您稍微插入代码:echo'$已修改。“->”。strotime($modified)。”;比较'$修改过。”有‘$前一天。“=>”。($已修改<$前一天)
代码>或类似的东西;输出是否发光?last\u modified
是一个类变量,它将返回类似于Sun的内容,2011年3月20日23:09:36 GMT
,然后我将其转换为timestamps
,在您对自己问题的评论中,您指出“last\u modified”是一个类变量,它将返回类似于Sun的内容,2011年3月20日格林尼治标准时间23:09:36“因此,我确信您现在必须更改您的“如果”条件。但确保没有时区差异总是好的:)谢谢威尔,两者都有。我只是输出$modified
时间戳
而不是将其放入变量中,并将其与我的云服务器在伦敦时间和我的PHP服务器在丹佛时间进行比较,现在工作得非常好!