scriptygoddess

15 Apr, 2002

Today in Years Past

Posted by: Lynda In: How to's|MT hacks

Some of you have been blogging or journaling for years and have seen other people do links at the bottom or side of their entries to "One Year Ago today" or "Two Years Ago today" or whatever. Most times, these links are hand-coded. Here is a way to automatically pull these links in using PHP and Moveable Type.

Let me start off by saying that this script was thought up by Amy over at domesticat.net for Greymatter. I simply modified it and expanded it for use in Moveable Type. Notes below will detail the changes and modifications I made.

You can see an example of how I have it setup to work over on this entry in my journal.

What this script essentially does is search a file for all entries made on the same date in previous years. So first, we'll need to make a new index file in MT that contains the information we'll later search.

Copy and paste this code exactly as it appears into a new index template. Be careful not to add any additional linebreaks or spaces, as it will break the script. Have it output to a file called entrylist.inc

What does that all mean? Glad you asked. Here's the content broken into colors, followed by an explanation:

<MTArchiveList><MTEntries><$MTEntryLink$>|<$MTEntryAuthor$>|<$MTEntryCategory$>|<$MTEntryDate format="%m/%d/%Y"$>|<$MTEntryTitle$>|<$MTEntryExcerpt$>
</MTEntries></MTArchiveList>

<MTArchiveList><MTEntries> … </MTEntries></MTArchiveList> – these are just the MT tags to make the list.
<$MTEntryLink$> – this will be replaced with the link to each entry.
<$MTEntryAuthor$> – the author of each entry.
<$MTEntryCategory$> – category of each entry.
<$MTEntryDate format="%m/%d/%Y"$> – This is what will be used to search for entries on previous dates. You can format the display dates to your liking later on, so leave this as it is.
<$MTEntryTitle$> – Title of each entry
<$MTEntryExcerpt$> – excerpt of each entry. If you use the excerpt field for something else in your entries, you can replace this with something like the following:

<$MTEntryBody convert_breaks="0" remove_html="1" trim_to="100"$>

This one is important:

| – This character is known as a "pipe" and is what the PHP script uses to pull apart all the information on each line so you can use each piece individually.

For example, the above code would output something like this, which is my entrylist.inc. Each line contains all the information for that particular entry. The php code will use character returns the separate each line into one entry and then it uses the pipe to separate each piece of information on that line to use later on.

Enough of that mumbo-jumbo. Here's the 2nd part of the script:

Copy this file into notepad or a text editor. Name it today.php and then make the following changes:

# What's the full server path to your entrylist file?
$entrylist = "/path/to/entrylist.inc";

Change this to your server path. For example, it might be:

/home/username/public_html/blogfolder/entrylist.inc

If you don't know the server path, check with your host.

# How do you want the months to be displayed? Edit the text between the
# quotes, but don't put in line breaks. Let the line wrap.

$month_names=array("01.", "02.", "03.", "04.", "05.", "06.", "07.", "08.", "09.", "10.", "11.", "12.");

Replace anything inside the quotation marks with how you want your months to display. "01." can be changed to "January", "Jan", "01", etc. Follow through with the rest of them.

echo "<a href= \"$entry_link \">$year: $subject</a><br/>$excerpt<br/>"

There are more detailed instructions above this line explaining what to do with it. You can format how you want your links to appear here. You can use the following variables:

$month, $day, $year, $subject, $author, $category

My suggestion would be to leave the code as it is the first time you try it out (making sure to change the path to the entrylist.inc) and then once you see what it does, go back and change it to suit your needs.

The last step in this is to include today.php in your Individual Entry Template and include variables to let the script know what day, month and year it is. Place this code in your Individual Entry Template wherever you'd like the links to appear:

<?
$this_month = "<$MTEntryDate format="%m"$>";
$this_day = "<$MTEntryDate format="%d"$>";
$this_year ="<$MTEntryDate format="%Y"$>";
include("/path/to/today.php");
?>

Make sure to change the bolded line to reflect the path to your today.php file.

Please let me know if there are any questions, as I've never been the best at explaining technical things. :)

——–

footnote: As mentioned, Amy wrote the original script for GM which can be found here. The original script pulls up all entries matching the date, regardless of year (same year entries are included). If this effect is prefered, you can modify the script to remove the following bit:

&& ($date_array[2] < $this_year)

Be aware this will include the CURRENT entry as well as any other entries posted on that day or that day in previous years.

The original script also had no way of including the excerpt, as GM stores that info separately. I modified this to include the excerpt.

The original script had a few bits of information that was needed for GM to process the script (like figuring out the four digit year based on the two digit year date) that I removed as it was not needed.

8 Responses to "Today in Years Past"

1 | kristine

April 16th, 2002 at 8:53 am

Avatar

Oh, this looks like a cool one to try out… :)

I have a question – I've seen scripts like this use .inc files, but I've never researched further to see why call it .inc instead of something else like .html that is a "normal" file?

2 | Lynda

April 16th, 2002 at 9:01 am

Avatar

Kristine,

Doesn't really matter one way or another what the extension is for these sorts of files. It's a personal preference. I hate "html" extensions and think they're ugly. I would use it only if it was a page that could survive outside of an include, which none should since includes should never contain html, head and body tags. :)

I call it inc because it's an include.

Again, it would work the same regardless of what you called it. Just depends on how you want to organize your site.

I'm sure the answer didn't need to be this long. :)

3 | kristine

April 16th, 2002 at 9:05 am

Avatar

:giggle: That all makes sense, Lynda; I'd seen other people use them and just wasn't sure if there was a specific reason or if it was preference like you said.

And then, having said that, I used to be able to use .phtml for my php pages on my first host, and I was such a snob because I liked how that extention looked so much more than .php. When they switched my settings and could no longer use .phtml, I was so annoyed. So I definitely understand extention-snobbery LOLOL!!!

And I'm sure this reply didn't need to be this long :g:

4 | Jade

May 26th, 2002 at 6:25 pm

Avatar

Thanks Lynda, that's very useful :)

5 | Jennifer

October 10th, 2002 at 9:55 pm

Avatar

Not sure if this works, but wanted to put a link to it for future reference…
I modified the script to display the entries from a year ago/month ago/week ago compared to the current day…
Modified file here

6 | Jennifer

October 23rd, 2002 at 8:37 pm

Avatar

Even better, here's another option… minimal server overhead – no other template needed – just need to have daily archives generated…

(Lynda, hope you don't mind me posting these… just using your post as my "personal scrapbook" keeper) :)

7 | Catherine

January 19th, 2003 at 6:14 pm

Avatar

Ooooo, I like this script! I just have one question… what happens if let's say, you didn't have an entry one or two years ago from the current day? What would display then?

8 | Jennifer

January 19th, 2003 at 11:14 pm

Avatar

Catherine – Using my modified version (see my comment just above this on 10/23) – it would display nothing. I have this script running on my site now, actually.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements