Last modified script
Ever felt like displaying when your page has last been modified (as in the footer of this page, for instance)? Look no further! Here's the code:
<?php
/*
Last modified: xx ago script
- http://korn19.ch/coding/last_modified.php
- Contact: http://korn19.ch/misc/contact.php
*/
$last_modified = time() - filemtime($_SERVER['SCRIPT_FILENAME']);
if(round($last_modified/(24*3600)) >= 1){ // over a day?
$last_modified /= (24*3600);
if(round($last_modified) > 6){ // at least a week?
$last_modified /= 7;
if(round($last_modified) > 3){ // .. months?
$last_modified /= 4;
if(round($last_modified) > 11){
$last_modified /= 12;
$output = round($last_modified).' years';
}
else{
$output = '~'.round($last_modified).' months';
}
}
else{
$output = round($last_modified).' weeks';
}
}
else{
$output = round($last_modified).' days';
}
}
else if(round($last_modified/3600) >= 1){ // At least one hour ago
$output = round($last_modified/3600).' hours';
}
else{
if($last_modified/60 < 1){ // barely a few secs ago
$output = $last_modified.' seconds';
}
else{
$output = round($last_modified/60).' minutes';
}
}
if(preg_match('/^1[^0-9]/', $output)){
$output = substr($output, 0, -1);
}
echo 'Last modified: '.$output.' ago';
?>
Simply add this in any file you want, as long as it is a .php or any other filetype that understands PHP.
The script gets the time when the current file has last been modified. Then, it checks if it's been over a second, minute, ... ago and displays the information with the proper unit, for instance:
If the file has been modified 3580 seconds ago, it'll display '1 hour ago' (the seconds get rounded), if it has been 72 seconds ago it will be '1 minute' ago and so forth.
Note: I have noticed that this code sometimes displays 'Last modified: 1 hour ago' on recently modified files. It seems that the error causing this is daylight saving time during the summer (thanks a lot, William Willett!). If you also have this problem, add this after the first line ($last_modified = time() - ...):
if($last_modified > 3590){
$last_modified -= 3600;
}

