On this day

For you Movable Typers who also use PHP, here's a little function that will let you link to content from previous years. Read on to see it.

This routine lets you link dynamically (or statically if you prefer) to blogs from prior years. Small caveat-- this requires that you archive entries on a daily basis. Here's the code-- just make necessary changes to the "archive_path" and "href_path" variables below and pop it into your template (assuming your template produces PHP files):

<?php
function on_this_day($month = 0, $day = 0, $year = 0) {
$years_to_check = 10;
$archive_path = "/path/to/your/archive/dir/";
$href_path = "http://www/path/to/your/archive/files/";
$file_ext = "php";

if (!$year)
$year = date('Y');
if (!$month)
$month = date('m');
if (!$day)
$day = date('d');

if ($month < 10)
$month = '0' . (int)$month;
if ($day < 10)
$day = '0' . (int)$day;

$month_day = '_' . $month . '_' . $day;
for ($i = 1; $i < $years_to_check; $i++) {
$file = ($year - $i) . $month_day . '.' . $file_ext;
$full_file = $archive_path . $file;
if (file_exists($full_file))
$ret .= "<a href=\"/past/$file\">" . ($year - $i) . "</a> ";
}
return $ret;
}
?>

Now, in order to use this, just write something like this:

<?php $past = on_this_day(); if ($past) { echo "On this day in: $past"; } ?>

Optionally, you can also pass a specific month, day and year to the on_this_day function. This is useful for your archived content where you always want to show the past entries for a given date.

Questions or comments about this? Post them here please.

1 Comments

Jason Mevius said:

I found your script on the Movable Type Support Forums and think it might work wonders for another project I'm considering.

I'm trying to force MT to be a day-planner/calendar. I've started a calendar already, but had a few sticking points before releasing it.

I would like to be able to seperate entries by categories and run this script on recurring dates.

For example, if I set my birthday, October 3rd as the category 'EveryYear', could this script be modified so it would just pick those entries up and place them every year?

At the same time, if I set an entry like a dentist appointment for a particular date, I don't want to be reminded that a year ago I had a dentist appointment.

Any ideas?

Jason

About

This article was published on March 4, 2002 11:02 AM.

The article previously posted was Blog me up, Scotty!.

The next article is index.swf?.

Many more can be found on the home page or by looking through the archives.

Powered by Movable Type