我正在尝试获取创建日期并查找日期差异,但未能完成。我能够使用new DateTime
来使用date1.diff
,因为我正在使用filemtime
从文件中获取日期。
有人能给我指正确的方向吗?谢谢
<?php
$_filename = realpath('test.txt');
if (file_exists($_filename)) {
$crdatefile = date("Y-m-d H:i:s",filemtime($_filename));
$date1 = date("Y-m-d H:i:s");
$datediff = $crdatefile - $date1;
echo '$datediff' . $datediff . '____';
if ($datediff < 0) {$datediff = $datediff * -1;}
echo '$datediff' . $datediff . '____';
$days = round($datediff / (60 * 60 * 24));
echo 'days' . $days . '____';
if ($days) {
echo 'days ' . $days . '____';
if ($days > 1) {
//unlink($_filename);
echo 'file delete' . '____';
}
}
echo 'file after delete' . '____';
}
?>
你可以很容易地做到这一点,但我会使用filectime
,因为在Windows上,这会给你实际的创建日期,而在Linux系统上,它会给你最后一次更改的日期,这是你能得到的最好的日期,因为Linux上不存在创建日期。
// create a new DateTime object with the timestamp returned by the file function
// pass it as format "U" (unix timestamp)
$fileTime = DateTime::createFromFormat('U', filectime($_filename));
// now just diff from a fresh DateTime object (date = now)
$daysSinceFileTime = $filetime->diff(new Datetime())->days;