Last Modified Script

Show when your page was last changed

The following PHP code allows you to show when a page has last been modified with the proper units (minutes, hours, etc.), such as in the footer of this page. Paste the code into a PHP file and select one of the usages to display when your file was last modified.
<?php
/* *************
 * Last modified: xx ago script
 * Show when a file was last modified
 * https://jalu.ch/coding/last_modified.php
 * ************* */

// ----------------
// Usage
// ----------------
// Output when this page was last modified
echo get_modified_line(); 
 
// Output when some file was last modified
echo get_modified_line("list.txt");
// You can use folders, too, just make sure to add "/." to the end
echo get_modified_line("folder/.");


// ----------------
// Functions
// ----------------
function get_modified_line($file=null) {
    if (!
$file) {
        
$date_modified filemtime($_SERVER['SCRIPT_FILENAME']);
    } else if (!
file_exists($file)) {
        
error_log("get_modified_line(): File $file does not exist");
        return 
'file not found!';
    } else {
        
$date_modified filemtime($file);
    }
    
$time_difference time() - $date_modified;
    return 
"Last modified: " time_ago($time_difference) . " ago";
}

function 
time_ago($diff) {
    
/* Thresholds t define the number of seconds the time difference should be
     * equal to or exceed for the unit to make sense. Since there is rounding
     * of output, we already use minutes as of 59.5 seconds (avoid outputting
     * "60 seconds").
     * Sizes s define the actual number of seconds for the unit. Months and years
     * are approximated with the rules given here below.
     * t(hour) = 59.5 * 60 = 3570;
     * t(day)  = 23.5 * 60 * 60 = 84600;
     * t(week) =  6.5 * 24 * 60 * 60 = 561600
     * 
     * s(year)  = 365.25 * 24 * 3600 = 31557600
     * s(month) = s(year) / 12 = 2629800
     * t(month) = s(month) * (11.5/12)
     * t(year)  = s(month) * 11.5
     */
    
$units = array('second''minute''hour''day''week''month''year');
    
$threshold = array(159.5357084600561600252022530242700);
    
$sizes     = array(160,   360086400604800262980031557600);
    
    
$thresh_size count($threshold);
    
$index 0;
    while (++
$index $thresh_size && $diff >= $threshold[$index]);
    --
$index;

    
$rounded_result round($diff $sizes[$index]);
    
$plural_s = ($rounded_result == 1) ? '' 's';
    return 
$rounded_result ' ' $units[$index] . $plural_s;
}

Expand/Decrease

Simply add this in any file you want with echo get_modified_line();

The function gets the time of a file's last modification and computes the difference to the current time in seconds. It passes this number of seconds to time_ago(), which formats the number with the most suitable unit (e.g. 3660 seconds will be displayed as "1 hour", 243 seconds as "4 minutes").

Converting Seconds to Other Units

Of course, you can use time_ago() separately to format numbers of seconds with the most suitable unit.

<?php
 $time 
mktime(000);
 
$time_diff time() - $time;
 echo 
"Midnight was " time_ago($time_diff) . " ago";

This produces: Midnight was 10 hours ago
(Server time: 10:17)

Tags

PHP last updated script, show file modification time, PHP display time since webpage last updated, last updated time ago, convert seconds to proper unit