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.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…