Getting a File's Last Modification Time

Problem

You need to know the last time a file was modified.

Solution

Use the File::lastModified() method.

$timestamp = File::lastModified($filename);
if ($timestamp === false)
{
    die("Failure getting the time");
}

The value returned is a Unix timestamp.

Discussion

If there's an error, a warning message is emitted.

It's a good idea to make sure the file exists before getting the modification time. See Determining If a File Exists.

comments powered by Disqus