PHP filemtime; file last modified time

Getting a file’s modified time with PHP is done by using the filemtime() function, this returns the files last modified time in Unix format.

An example below which also checks if the file exists:

$file = 'info.txt';
if (file_exists($file)) {
    echo filemtime($file);
}

will output: 1589463479.

To get a readable date-time format use the date() function to format as you please.

$file = 'info.txt';
if (file_exists($file)) {
    echo "$file was last modified " . date("Y-m-d H:i:s", filemtime($file));
}

Outputs info.txt was last modified: 2020-05-14 23:37:59.

If file older or newer than

Now checking if a file is within the past 24 hours or older than 24 hours:

$file = 'info.txt';
$diff = time() - filemtime($file);
if ($diff > 86400) {
    echo 'Older than 24 hours';
} else {
    echo 'Newer than 24 hours';
}

As time() returns the current Unix timestamp this can have the file modified time subtracted from it to find the difference, 86400 seconds is 24 hours.