This is old content! The graveyard is a snapshot of content created from 2002-2005. For new stuff, visit Maniacal Rage.
 

Relative Dates Using PHP

9 comments (closed), posted on february 26, 2004, tags: movable type

So you want to jump on the relative dates bandwagon? Here's how you can do it quickly and easily. This tutorial will assume you're using Movable Type, but you can use the PHP bit on its own if you wish*. You could also do this via SQL, but in this case it's easier to use a function than to edit how MT queries the DB. First, let's talk about the PHP function.

The idea here is simple: You pass the function a date stamp in a specific format (YYYYMMDDHHMMSS) and it converts that stamp into a Unix timestamp (in seconds since the Epoch). It then finds the difference between the current time and that timestamp. This number is the number used to calculate the relative values. It works as such:

  • A minute contains 60 seconds.
  • An hour contains 60 minutes (3600 seconds).
  • A day contains 24 hours (86400 seconds).
  • A week contains 7 days (604800 seconds).
  • A month contains 30 days (2592000 seconds).
    Obviously, not all months contain 30 days. This is an average and will provide results that are reliable enough in this case.

The function works from the largest timeframe down. If the amount of months is 1 or greater, then the function will simply return the date in MM/DD/YYYY format. The reason for this is that you don't want someone looking your archive from a year ago and seeing a posted date of 1 year, 3 months ago. How would they know when exactly that was? If a month has not gone by, the function moves down to weeks. The way the function builds results is as follows:

  • If there are 1 or more months, return just a date.
  •      Otherwise:
  • If there are 1 or more weeks, return weeks and, if necessary, days.
  •      Otherwise:
  • If there are 1 or more days, return days and, if necessary, hours.
  •      Otherwise:
  • If there are 1 or more hours, return hours and, if necessary, minutes.
  •      Otherwise:
  • If there are 1 or more minutes, return minutes.
  •      Otherwise:
  • Just return seconds.

This means you'll never see something like 4 weeks, 3 days, 8 hours, 32 minutes, 11 seconds returned. That's just crazy. This limits each set of time to one (or no) subset. Now then, let's put it all together.

First, click here to see and copy the source code for the PHP function. Copy that code and put it into your functions file (or, if you're completely unorganized, create a new file) and make sure you include it on all the templates you wish to use these dates on (like such: <?php include('functions.php'); ?>, where functions.php is the file that contains the function). Then, where's you want the date to appear, do this:

Posted <?=doRelativeDate('<$MTEntryDate format="%Y%m%d%H%M%S"$>');?>

Note that the function automatically adds the text ' on' when it prints only a date and ' ago' when it prints a relative date. Hence my addition of the word 'Posted' and the space after it in the code above. The function doesn't automatically print the 'Posted' part because some people might want it to say 'Written' or 'Barfed' or something. Rebuild and you've got yourself some relative dates, pal.

* If you want to use this without Movable Type, you can simply substitute what I passed to the function above with your own properly formatted date stamp (see mktime(), date(), etcetera).

Blacklisted

posted on january 26, 2004, tags: movable type

After a very long time of forgetting to/being lazy about installing Jay Allen's MT-Blacklist plugin, I've finally done it today. Hopefully this will eliminate the endless supply of spam comments and trackbacks I get here. If you find yourself unable to comment for some reason (and you're not trying to spam me), send me an email and let me know.

Today seems to be anti-spam day.

MT Users Apparently Bad Dates

2 comments (closed), posted on august 11, 2003, tags: movable type

This not surprising, but apparently Movable Type users are also bad dates. One might make the assumption based on their enjoyment of something related to technology—which usually means their non-enjoyment of time spent with the opposite sex. Case in point this evening:

I showed up at the Scratcher for the first International Movable Type Meetup. It was supposed to be at 7PM. I got there early, having headed over after work at six. I waited until 7:40PM, and no one showed.

It's bad to be stood up by a date, but pathetic to be stood up by a bunch of personal-website-writing nerds. Boo.

I'm a Lousy Host!

posted on august 11, 2003, tags: movable type

I completely forgot about today's International Movable Type Meetup. Lucky I got an email over the weekend, since I'm a damned "host" and I would have completely forgotten about the event otherwise. If you want to come tonight, please visit Movable Type Meetup to register (free). That link is for anyone in any city, as Meetup automatically places you in the nearest group. The event takes place tonight, August 11, at 7:00PM (local time).

In NYC, we're meeting at: The Scratcher, 209 E 5th St, New York, NY [map]

I suggest signing up and stopping by. Could be fun, right? And if not, at least they serve alcohol so that you may drink yourself into enjoyment.

Easy Friend Links

3 comments (closed), posted on march 4, 2003, tags: movable type

Note: This is really outdated and I don't suggest you use it. It can slow down MT quite a bit.

I've received plenty of requests lately to write a quick tutorial on how I use MTMacros to automate common friend links on this site. For instance: Shawn Morrison, a complete link to Shawn's website, is created by simply typing shawnmorrison in the MT entry textarea. Simple and efficient.

This is based on the tutorial I wrote before, called MT Macros & Acronyms (which has been updated, by the way, to be more efficient), but only in principal. You can use this tutorial completely separate from that one, or vice versa.

» Continue reading Easy Friend Links

Better Category Sorting

posted on march 2, 2003, tags: movable type

Right, so I was supposed to write that tutorial about category sorting using PHP. I must have forgotten. Better late than never, I suppose. Here goes: Ellie was creating a new section for her website—a photo blog—and came across a bit of a problem.

She wanted the front page of this section to show the most recent photo from each category. The important thing was that these photos were listed by last update, descending. That means newest photo on top, oldest photo on bottom. The problem is, MT doesn't seem to allow you to organize categories in this fashion. No matter how hard I tried, I couldn't get them to display in any order other than alphabetically by label. That wasn't going to work, so I set out to find a solution.

» Continue reading Better Category Sorting

MT Macros & Acronyms

3 comments (closed), posted on february 21, 2003, tags: movable type

A few people have asked me how I do the acronyms on this site. You know, the words that have the grey dotted underline and when you mouse over them you get a tooltip telling you what it stands for? Yes, those. It took me a while to find the best way to do it, so I'll go ahead and describe that here. Due credit to Mark Pilgrim and Brad Choate.

» Continue reading MT Macros & Acronyms

Sanitize Usage

5 comments (closed), posted on february 21, 2003, tags: movable type

Based on some changes I made around here, and a question posed by Shawn Morrison last night, I'm gonna post a few quick notes about Brad Choate's Sanitize plugin for Movable Type. Because this plugin was packaged with MT 2.6x, and because it's turned on my default, a lot of people either don't know it's doing anything, or don't know how to effectively change what it's doing for the better. Brad's simple description of Sanitize is:

Sanitize is a Movable Type plugin that allows you to clean HTML and other markup that might exist in an comment entry.

Basically, you tell Sanitize which HTML tags to allow, and it will destroy the rest. It's a great idea (especially for security's sake), and works well once you get past the learning curve. By default, Sanitize will allow the following HTML tags:

a href, b, br, p, strong, em, ul, li, blockquote

This allows for the most common of tags, although I think most people will question why br and p are in there. There is a reason: Sanitize cleans HTML tags in comments regardless of whether or not you have "Convert Line Breaks" turned on in your comment configuration. If you do, you know that your comments contain proper p and br tags. Well, if these tags weren't in your allowed list, all of your entries would be stripped of these tags... not a good thing. So leave those in there. But it's important to discuss something related to that br tag...

Do you see that a href tag up there? See how it says a and then it has a space and then says href? Those aren't two different tags. Sanitize is smart enough to understand you if you tell it a tag has multiple attributes. But you do actually have to tell it. If you had just said a up there, instead of a href, then all of your links would be stripped of their href attributes (rendering them quite useless, yes?). This must also be done for empty elements in XHTML that require the closing slash (/). Like, for instance, a br tag. Up there in the default list, it's just br. But I (and anyone using XHTML) use <br />. So, in Sanitize, I need to specify that slash by saying br / instead or just br. Make sense?

Now then, the biggest issue: that missing target attribute. 99% of MT users who upgraded to MT 2.6x didn't notice that any link posted in a comment started opening in the same window after the upgrade. This is because Sanitize's default allowed link tag, a href, doesn't allow for the target attribute. To fix this, change your a href allowance to a href target. Simple! Also note that you can add title too, if you wish, to allow for really complete links (eg: a href target title).

So then, we've now got an updated list of allowed tags that fits most common usage. Here it is:

a href target title, b, br /, p, strong, em, ul, li, blockquote

I use a slightly different list (I don't allow lists or blockquotes), but this should work for almost anyone. And in case you're not sure where this goes—in your MT admin area, under Blog Config, in Preferences, look in the General Settings section near the bottom (it's above your welcome message).

Also, if you don't like Sanitize at all, you can turn it off. You'll need to place something inside your <$MTCommentBody$> tag, though. To disable Sanitize, change that tag to: <$MTCommentBody sanitize="0"$> and you'll be all set.

Graphical Date Tutorial

16 comments (closed), posted on january 12, 2003, tags: movable type

I have a feeling eventually someone who uses MT will ask me how I did the dates on this site. In a preemptive response (and also because it's something I think people would use if they knew how to do it), I have compiled a simple tutorial. To do this, all you need is to be running Movable Type. Many thanks to James who pointed out how complicated I made this the first time around. The tutorial has been updated and is significantly easier...

» Continue reading Graphical Date Tutorial


See Also

View the archive

Original iPod Introduction
How far we've come in just a few short years. Here's where it all started.

Front Row on Non-iMacs
Going to try this tonight!

WriAShorStorWe!
DY starts a one-week short story writing event for people to lazy to enty NaNoWriMo. VerCooIdea.

Lost Rhapsody
Funny Flash movie using Weird Al music and Lost stuff. Lyrics make a surprising amount of sense!

Jed's Other Poem
Unsolicited music video made on an Apple ][. Fantastic!

Printers Output Secret Barcode
The government is keeping tabs on what you print, with the help of major printer companies.

Dreamhost Promo Codes
DH already has very cheap, very good hosting—this just sweetens the deal.

Photos of the new iPod
Just received my new iPod and I put a few photos up.

PEZ MP3 Player
Funny idea that actually looks kind of neat. I like that it comes pre-loaded with "indie" music.

HD Easter Egg
"My Name is Earl" on NBC gives viewers with HD TVs a little easter egg. Cute, but weird.