Hacks: “On this day” links in Movable Type

06/24/2003

Each day webloggers across the world post news, comments and little fragments of personal information onto their sites. And everything that they post will be forever associated with that specific day in history. But they’re not the only sites to connect a piece of writing or a picture with a day. In fact all over the internet there are hundreds of ‘[something] of the day’ or ‘on this day’ sites – from “Astronomical Picture of the Day” through to “Dilbert Cartoon of the Day”. There’s a whole category on Yahoo dedicated to these things.

This hack allows you to put an automated link on the bottom of each of your posts to the Dilbert cartoon (or astronomical picture, word of the day etc.) that was published on that day. You can use it to add a little context to the events on your site or just to show off your interests.

First things first – what are we trying to link to? These sites often have simple URLs that are based upon the date on which they were initially displayed. For example the “Astronomy Picture of the Day” for February 23rd 2003 has the URL:

antwrp.gsfc.nasa.gov/apod/ap030209.html

…where the six numbers near the end are the year (03), the month (02) and the day (09). The Dilbert cartoon for the same day has this URL:

dilbert.com/comics/dilbert/archive/dilbert-20030209.html

…which uses almost exactly the same format except with a the year spelled out in full (2003).

So in order to insert these links on a day-by-day basis, we’re going to have to put the basic URL in place without the date elements, find some way of inserting those date elements and make sure that they’re formatted so they’ll work as a link. We’re going to do this by using some of Moveable Type’s most useful and versatile features – the <$MTEntryDate$> tag. If you insert this tag into your templates by itself it will use its default setting – which is designed for reading and will look a bit like this: “September 9, 2003 11:44 PM”. But you can easily override this by using the format attribute and one or more date-tag variables. Here are a couple of examples of how you might format <$MTEntryDate$> and what the result would look like on your published page:

<$MTEntryDate format="%d %b %y"$>
would look like "09 Sep 03"
<$MTEntryDate format="%Y: %B, %e"$>
would look like "2003: September, 9"

Here are what some of those letters mean:

Month:
%b - name abbreviated to three characters
eg. Sep
%B - name in full
eg. September
%m - presented as two digits padded with a 0 if necessary
eg. 09
Day:
%d - two digits padded with a 0 if necessary
eg. 09
%e - two digits padded with a space if necessary
eg. 9
Year:
%y - two digits padded with a 0 if necessary
eg. 01
%Y - four digits.
eg. 2001

So to make those daily URLs all we have to do is change the original URLs to include the <$MTEntryDate$> tag like so:

From:

http://antwrp.gsfc.nasa.gov/apod/ap030209.html

to:

http://antwrp.gsfc.nasa.gov/apod/ap<$MTEntryDate

format="%y%m%d"$>.html
From:

http://www.dilbert.com/comics/dilbert/archive/dilbert-

20030209.html
to:

http://www.dilbert.com/comics/dilbert/archive/dilbert-

&lt$MTEntryDate format="%Y%m%d"$>.html

So this is what you’d put into your template:

<a href="http://antwrp.gsfc.nasa.gov/apod/ap
<$MTEntryDate format="%y%m%d"$>.html">
Astronomical Picture of the Day</a>

This hack was originally supposed to appear in the ill-fated O’Reilly “Blogging Hacks” book. I’ll be putting all my contributions online over the next few days / weeks.