How to determine file age using PHP?

if (time()-filemtime($filename) > 2 * 3600) {
  // file older than 2 hours
} else {
  // file younger than 2 hours
}

You can use filemtime function to get the last modified date/time and use that to see how old the file is.


You are looking for the filemtime function

<?php
// 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));
}
?>

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.