I created this code to get the date a file was last touched then display it to the user in AM/PM format.
It doesn’t seem to be working though. I know I’m close; what am I doing wrong?
$filename = 'test.html';
if (file_exists($filename)) {
$date = date(filemtime($filename));
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', strtotime($date));
Output: last updated: December 31 1969 06:59 PM
Try this:
In your code, this line:
wouldn’t work since
filemtimereturns a UNIX timestamp, which you are then passing as the first parameter todate(). Even if that did work, you are then converting that date back to a UNIX timestamp withstrtotime(), and then back into a date string again which seems a little inefficient.Also consider what happens if the file doesn’t exist, will
$datehave been set elsewhere in your code?