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.
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.
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Some early September spring shots from a bushwalk, It is still too early for wildflowers.
I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…
If you ever needed a simple solution in Laravel to delete an entire directory and…
How to enforce only one user to register or be registered in a Laravel web…