Here's a similar snippet of code, this time in JavaScript, which will output the date that a page was last modified in human readable format. See https://secure.kitserve.org.uk/pete/ with JavaScript enabled for an example of it in use. Note that the suffix is only correctly generated for numbers less than 100, for example '111' would be turned into '111st'! This isn't a problem for dates, of course, but you'll want to bear it in mind if you're using the code for more general numbers. Generalising the code to fix this issue would be trivial, for example by adding something along the lines of
var num_remainder = day%100;
and changing the if and switch statements to operate on num_remainder instead of day.
// Written for http://www.kitserve.org.uk/ on February 18th, 2010 and released into the public domain.
// This script outputs the date that the current document was last modified in human readable format.
// You are welcome to use it yourself, links back to kitserve.org.uk would be appreciated!
// Initialise variables:
var fulldate = new Date(document.lastModified);
var day = fulldate.getDate();
var month = fulldate.getMonth();
var year = fulldate.getFullYear();
// Set the names of the months in an array:
var monthname = new Array();
monthname[0] = 'January';
monthname[1] = 'February';
monthname[2] = 'March';
monthname[3] = 'April';
monthname[4] = 'May';
monthname[5] = 'June';
monthname[6] = 'July';
monthname[7] = 'August';
monthname[8] = 'September';
monthname[9] = 'October';
monthname[10] = 'November';
monthname[11] = 'December';
// Set a suffix for the day number:
var day_suffix = 'th';
if (day < 10 | day > 20)
{
switch (day%10)
{
case 1:
day_suffix = 'st';
break;
case 2:
day_suffix = 'nd';
break;
case 3:
day_suffix = 'rd';
break;
default:
break;
}
}
// Output the date:
document.write('This page was last updated on ' + monthname[month] + ' ' + day + day_suffix + ', ' + year + '.');
Alternatively, you can copy the code from https://secure.kitserve.org.uk/pete/scripts/lastupdate.js - please don't hotlink to it, if you want to use the code on one of your own sites copy the .js file across. As with the PHP example above, any comments are welcomed.
Here's a similar snippet of code, this time in JavaScript, which will output the date that a page was last modified in human readable format. See https://secure.kitserve.org.uk/pete/ with JavaScript enabled for an example of it in use. Note that the suffix is only correctly generated for numbers less than 100, for example '111' would be turned into '111st'! This isn't a problem for dates, of course, but you'll want to bear it in mind if you're using the code for more general numbers. Generalising the code to fix this issue would be trivial, for example by adding something along the lines of
and changing the
ifandswitchstatements to operate onnum_remainderinstead ofday.Alternatively, you can copy the code from https://secure.kitserve.org.uk/pete/scripts/lastupdate.js - please don't hotlink to it, if you want to use the code on one of your own sites copy the .js file across. As with the PHP example above, any comments are welcomed.