On sites where the homepage is not the main page of the blog, it isn't always easy to tell when the last update was, and for those of us whose sites are built that way, it can sometimes be a pain to keep the "Last Updated: #########" line up to date. I realized a while back that since I generally write a blog entry of the "hey I added something to {link}this page{/link}" sort even when my updates weren't directly blog content related, so I built this little PHP script that keeps my last updated tag correct without my even thinking about it, since it gets it from the most recent blog entry and pastes it into any page on my site. I use Greymatter, but I don't think it would be too hard to modify this to any other blog program.
First you get the entrylist from greymatter as an array (each entry is on a seperate line of the file, and becomes a seperate array item). Be sure to change the line below to your server path.
$entry_List = file ('/server/path/to/your/gm-entrylist.cgi');
Greymatter inserts the data for each new entry at the beginning of the file, so the most recent entry will be the first line of the file
$last_Entry = $entry_List[0];
The information in that line will be entry number, author, entry title, date/time, etc seperated by pipes (that's this character: | ), and we only need the date for this, so we turn that line into an array, splitting on the |s.
$entry_Paramaters = explode ("|", $last_Entry);
The date is the fourth piece of data (but arrays start counting at 0) so:
$entry_Date = $entry_Paramaters[3];
Then we split again to seperate the date into day month and year. This time split on the / because Greymatter keeps the date as mm/dd/yy (you don't have to do the rest if you want to keep the date in that format, you could just echo it here, but I wanted to be able to change it)
$date_elements = explode("/" ,$entry_Date);
Then we turn those pieces into a timestamp so we can use it.
$mytimestamp = mktime (0, 0,0 ,$date_elements [0], $date_elements[ 1],$date_elements [2]);
And spit the date out formatted however we like using the date function of PHP.
PHP date formating:
a – "am" or "pm"
A – "AM" or "PM"
d – day of the month, 2 digits with leading zeros; i.e. "01" to "31"
D – day of the week, textual, 3 letters; i.e. "Fri"
F – month, textual, long; i.e. "January"
h – hour, 12-hour format; i.e. "01" to "12"
H – hour, 24-hour format; i.e. "00" to "23"
g – hour, 12-hour format without leading zeros; i.e. "1" to "12"
G – hour, 24-hour format without leading zeros; i.e. "0" to "23"
i – minutes; i.e. "00" to "59"
j – day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') – day of the week, textual, long; i.e. "Friday"
L – boolean for whether it is a leap year; i.e. "0" or "1"
m – month; i.e. "01" to "12"
n – month without leading zeros; i.e. "1" to "12"
M – month, textual, 3 letters; i.e. "Jan"
s – seconds; i.e. "00" to "59"
S – English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t – number of days in the given month; i.e. "28" to "31"
U – seconds since the epoch
w – day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
Y – year, 4 digits; i.e. "1999"
y – year, 2 digits; i.e. "99"
z – day of the year; i.e. "0" to "365"
Z – timezone offset in seconds (i.e. "-43200" to "43200")
I wanted mine formatted like "January 1, 2003" so I used:
echo "Last Updated: ";
echo date (" F j, Y", "$mytimestamp");
Here's just the code, without the explination (don't forget to change the server path, and date format):