Automated WEB Page Date Stamping

This brief tutorial covers another of my most common questions, which is "how do I add a automatic date stamp to my pages?", well this page covers the Perl needed to do just this. As for why automate this? To answer this, ask yourself have you ever forgotten to update a manual date stamp?, I sure have, and whats the betting I am not alone.....

Requirements

As expected to be able to use these examples you need to have a working Perl installation, and as everything here is aimed at web use, a functional apache web server would also be a good thing to have running. Now the setup and configuration of both of these is something thats way outside the scope of this document, but here are some sites and books that may help you.

Apache WEB Server Home [apache.org]
Apache the Definitive Guide [oreilly.com]
Learning Perl [oreilly.com]
Perl Programming, Third Edition [oreilly.com]

NOTE: My intention with writing this page was not to simply create yet another perl tutorial, there are many of them available, but to cover the application of the code, which is why at no point do I go into the inner workings of the functions used or any syntax etc. If you need help with perl programming I strongly recommend you get one of the titles listed above, or at the very least hit google.

Overview

As you would expect the first thing to do is to retrieve the "last modified date" for the file we are going to date stamp, which is done using nothing but the standard Perl library. The code snippet looks like this.

($day,$month,$year)=(localtime( (stat($filename))[9] ))[3..5];
$Date = sprintf("%2d-%02d-%04d",$day,$month+1,$year+1900);

Looks complex yes, well its not as bad as it looks. The first line retrieves the day, month and year from the filename specified in the $filename variable, then the second line formats the results into the dd-mm-yyyy format, after applying corrections to the month and year.

While this is all well and good, the dd-mm-yyyy format is not very user friendly, especially for use on a web page, so the next step is to convert it into what I call "human friendly" format. Which I do with the following snippet.

my $InDate = "dd-mm-yyyy";

my @Months = ('January',
          'February',
          'March',
          'April',
          'May',
          'June',
          'July',
          'August',
          'September',
          'October',
          'November',
          'December');
my ($InDay,$InMonth,$InYear)=split(/-/,$InDate);
my $Human = ($Months[$InMonth-1]." ".$InDay.", ".$InYear);

This snippet is actually about as simple as it gets, first you define the array @Months loaded with the months, then split up the date to convert into its component parts, then finally format the result into Month Day, Year, after applying the correction factor for the Months array to account for the element numbering starting at zero.

At this point, we have two separate snippets that are not much use on their own, so now its time to put them together into something useful (yes the way I use them for this site), which includes automated filename collection.

#!/usr/local/bin/perl -w

my ($Source)        = $ENV{"REQUEST_URI"};
my ($Path)          = $ENV{"DOCUMENT_ROOT"};
my ($Filename)           = "$Path/$Source";
my ($Day,$Month,$Year)   = (localtime( (stat($Filename))[9] ))[3..5];
my (@Months)        = ('January',
                  'February',
                  'March',
                  'April',
                  'May',
                  'June',
                  'July',
                  'August',
                  'September',
                  'October',
                  'November',
                  'December');

$Year = $Year+1900;

####################################################################################
# display the status bar with the last updated date, and top link
####################################################################################
print "Content-type: text/html \n\n";
print <<HTML
<tr>
  <td>&nbsp;</td>
  <td style='padding:0 5px;'>

     <table cellpadding='0' cellspacing='0' width='100%'>
       <tr>
         <td width='50%' align='left' style='font-size:10px;color:#000000;'>
           Updated: $Months[$Month] $Day, $Year
         </td>
         <td width='50%' align='right'>
           <a style='font-size:10px;color:#000000;' href='#top'>Top</a>

         </td>
       </tr>
     </table>
  </td>
</tr>
HTML

As you can see there is not much different in the above when compared to the first two snippets of Perl, with the exception of the HTML the main difference is the use of two environment variables.

Variable Function
REQUEST_URI Contains the filename and path of the calling script, relative to the root of the web server
DOCUMENT_ROOT Contains the path to the root of the web server, relative to the servers physical root

These two variables when used together provide the full path to the calling script, relative to the servers physical root directory, which is real important as Perls stat function would not be able to find the file without it, as the REQUEST_URI is relative to the web server root, not the physical root.

I take it the HTML needs no discussion, its pretty simple stuff and fairly specific to my application, with the only area that I need to cover is the actual display of the Last modified date.

The perl code sets the following variables, which you can simply display in any format that suites your application.

Variable Function
$Months[$Month] The month in human friendly format, taken from the @Months array, with element $Month
$Day Numeric day of the month, with the leading zero (if any) removed.
$Year Four digit year

Thats it, all done, with the results being shown at the bottom of every page on this site. See nothing to it, and all done without any need for cgi.pm or any other external modules.

Valid XHTML 1.0 Strict