Skip navigation.

Page Last Changed PHP Code

Here is the PHP code that produces the page last changed message on each page.

<?php
/*
 * date.php - create 'Page last changed . . . ago'.
 *
 * This script is based on quirksmode.js by Peter-Paul Koch,
 * see http://www.quirksmode.org/
 *
 * date: 2 nov 2005
 * authors: Peter-Paul Koch, Martin Moene
 */

/**
 * return 'Page last changed [today/yesterday]', or
 *        'Page last changed [two..fourteen/dd] [days/weeks/months/years] ago'.
 */
function lastChanged( $filename )
{
   $dt       = getdate( time() );
   $midnight = mktime( 24, 00, 00, $dt["mon"], $dt["mday"], $dt["year"] );
   $daysago  = ( $midnight - filemtime( $filename ) ) / 60 / 60 / 24;

   if ( $daysago < 0 )
      return '[future date]';

   $unit = 'days';
   if ( $daysago > 730 )
   {
      $unit    = 'years';
      $daysago = floor( $daysago / 365 );
   }
   else if ( $daysago > 60 )
   {
      $unit    = 'months';
      $daysago = floor( $daysago / 30 );
   }
   else if ( $daysago > 14 )
   {
      $unit    = 'weeks';
      $daysago = floor( $daysago / 7 );
   }
   else
   {
      $daysago = floor( $daysago );
   }

   $towrite = 'Page last changed ';

   if ( $daysago == 0 )
   {
      $towrite .= 'today';
   }
   else if ( $daysago == 1 )
   {
      $towrite .= 'yesterday';
   }
   else
   {
      $nums = array(
         'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
         'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen' );

      if ( $daysago < count( $nums ) )
      {
         $towrite .= $nums[ $daysago ] . ' ' . $unit . ' ago';
      }
      else
      {
         $towrite .= strval( $daysago ) . ' ' . $unit . ' ago';
      }
   }

   return $towrite;
}

?>