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>
</div>
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”
Rock on, man. This is awesome and works perfectly with WordPress 3.1.1. Thanks! 🙂
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
Thank you very much – that’s awesome 🙂