A Non-Stupid “Time Since” Plugin for WordPress

On my blog, I like post and comment dates to include an “....ago” description. I've not cared for any of the simpleton solutions I've seen so far, so I wrote my own plugin for WordPress to provide this.

The problem with most time-span computations I've seen is that they simply subtract one time from another to get a total number of elapsed seconds, then start dividing to get the time-unit components (e.g. divide by 60 to get the elapsed number of minutes). This is fine for durations up to 28 days, but past that the definition of “one month” is dependent on the specific dates involved.

My plugin handles this computation properly, and also brings some common sense into how time spans are reported. For example, if something happened “1 hour, 17 minutes ago”, the extra 17 minutes is a lot more relevant than in “22 hours, 17 minutes”. My plugin reports the latter simply as “22 hours”.

Of course, when something happend a few weeks or months or years ago, hours and minutes are not relevent at all, so they're not even shown.

Here it is:   jeffreys-timesince.php v1.1

My plugin is a drop-in replacement for what I used to use, Dunstan Orchard's “Time Since” plugin, which is no longer supported.

Here's how I use it, from my post.php theme file:

 <div class="post_timestamp">
   <span class="post_date"><?php the_time('F jS, Y') ?></span>
   <span class="post_time"><?php the_time('g:ia') ?></span>
   <span class="post_tz">JST</span>
   <span class="post_ago">(<?php echo time_since(abs(strtotime($post->post_date_gmt . " GMT")), time()); ?> ago)</span>

Your timestamp section likely looks different (WordPress didn't handle timezones well when I set up my blog, so I had to force some things; hopefully it's better now), but the highlighted section can be dropped in wherever you like within the loop.

It's used in a similar way in other files that display timestamps, such as index, archive, and comment pages.

I've published one other WordPress plugin: “File-based Posts

All 3 comments so far, oldest first...

Rock on, man. This is awesome and works perfectly with WordPress 3.1.1. Thanks! 🙂

— comment by Raam Dev on April 12th, 2011 at 6:11am JST (13 years, 3 months ago) comment permalink

This sounds like exactly what I need – but the php file is downloading 0 bytes for me.
Oops, sorry about that. It should work now. I took the opportunity to update to the latest version as well. —Jeffrey

— comment by Kay on October 8th, 2011 at 10:20am JST (12 years, 9 months ago) comment permalink

Thank you very much – that’s awesome 🙂

— comment by Kay on October 8th, 2011 at 10:00pm JST (12 years, 9 months ago) comment permalink
Leave a comment...

All comments are invisible to others until Jeffrey approves them.

Please mention what part of the world you're writing from, if you don't mind. It's always interesting to see where people are visiting from.

IMPORTANT:I'm mostly retired, so I don't check comments often anymore, sorry.

You can use basic HTML; be sure to close tags properly.

Subscribe without commenting