Display the last modified date
By: Jool
Learn how to fetch and display the modified date of pages
Are you tired of manually updating the last modified date displayed on your site? Using PHP, it is easy to automate this task, for any file you have, with just a few lines of code.
<?php
$filename = 'filename.php';
if (file_exists($filename)) {
echo "$filename was last modified: " . date ("M d Y at H:i:s.", filemtime($filename));
}
?>
To explain the above, we have to define the file we want to check; in this case filename.php is the file. You can use any extension. After we have established that, we have to check if the file actually exists. If it exists, then using the filemtime() function, we check the last modified date and then we display it with date(). It will be displayed like this (of course the date will be the one that is fetched);
filename.php was last modified: Feb 16 2007 at 23:07:59
There are a few notes about this. As you might expect you can customize this to however you want, using different date formats or the way it is displayed. You have to be aware that checking the date for large files, as in, over 1gb or more, will most likely cause the script to take a long time to execute or if the file is really massive it might even not work at all, so be aware of that. Also, this will display the date that the file was last saved, even if you didn’t actually edit it, it will always show the time the file was last saved. Other then those notes, it is very useful all around and saves a lot of time over manually editing the last edited date.
|