php 如何使用PHP确定文件年龄?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1757097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine file age using PHP?
提问by anneb
Any way to determine file age of an image in a folder using PHP?
有什么方法可以使用PHP确定文件夹中图像的文件年龄?
I want to delete older files than 2 hours, is this possible without adding timestamp-names to their filenames on upload to the folder?
我想删除超过 2 小时的旧文件,是否可以在上传到文件夹时不将时间戳名称添加到文件名中?
If so, please give me an example!
如果是这样,请给我一个例子!
thanks
谢谢
采纳答案by Marek Karbarz
回答by anneb
if (time()-filemtime($filename) > 2 * 3600) {
// file older than 2 hours
} else {
// file younger than 2 hours
}
回答by Roch
回答by Gerfried
Be aware that on NAS/SAN storages the file time of an up-to-date file might be different from the PHP time(), as those storages might have their own clock. I think such configurations are rare, but it cost me some headache.
请注意,在 NAS/SAN 存储上,最新文件的文件时间可能与 PHP time() 不同,因为这些存储可能有自己的时钟。我认为这样的配置很少见,但这让我有些头疼。
回答by Pascal
I digged a bit on the web and Stackoverflow (PHP How to find the time elapsed since a date time?) and here is my solution:
我在网络和 Stackoverflow上挖掘了一下(PHP 如何查找自日期时间以来经过的时间?),这是我的解决方案:
It's a full answer giving a human readable representation of file age + a check to see if the file is 2 hours old or not.
这是一个完整的答案,给出了文件年龄的人类可读表示+检查文件是否为 2 小时。
<?php
function humanTiming ($time)
{
// to get the time since that moment
$time = time() - $time;
// time unit constants
$timeUnits = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
// iterate over time contants to build a human
$humanTiming;
foreach ($timeUnits as $unit => $text)
{
if ($time < $unit)
continue;
$numberOfUnits = floor($time / $unit);
// human readable token for current time unit
$humanTiming = $humanTiming.' '.$numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
// compute remaining time for next loop iteration
$time -= $unit*$numberOfUnits;
}
return $humanTiming;
}
$filename = '/path/to/your/file.extension';
if (file_exists($filename))
{
echo 'Now = '.date ("Y-m-d H:i:s.", time());
echo "<br/>$filename was last modified on " . date ("Y-m-d H:i:s.", filemtime($filename));
$time = strtotime('2010-04-28 17:25:43');
$time = filemtime($filename);
echo '<br/>File age is '.humanTiming($time).'.';
$fileAge = time()-filemtime($filename);
echo '<br/>Is file older than 2 hours? '.(($fileAge < (2*3600))?'No':'Yes').'.';
}
?>
Here is the output on my side:
这是我这边的输出:
Now = 2013-01-29 09:10:12.
/path/to/your/file.extension was last modified on 2013-01-29 08:07:52.
File age is 1 hour 2 minutes 20 seconds.
Is file older than 2 hours? No.
回答by Karol
You can use date/time of last file modification:
您可以使用上次文件修改的日期/时间:
回答by Dave DeLong
I'd check out the filemtime()or the filectime()functions. Those will give you the modified and creation times (respectively) of the file.
我会检查filemtime()或filectime()功能。这些将为您提供文件的修改时间和创建时间(分别)。
回答by Gabriel Sosa
Try with filemtime()
试试 filemtime()
This function returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed.
int filemtime ( string $filename )
filename: Path to the file.
该函数返回文件的数据块被写入的时间,即文件内容被改变的时间。
int filemtime ( string $filename )
filename: 文件路径。
// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}

