Over the weekend while I was working on the blog re-design I decided that in the new design I’d use more relative dates rather than actual dates when displaying an entries publication date and a comment’s date.
Especially with comments, I’d much rather know that it was posted a month ago than ‘May 23, 2008 at 9:42am’.
I first decided on the time ranges:
- a moment ago (less than 1 min)
- 1 min – 59 mins
- 1 hour – 23 hours
- 1 day – 7 days
- 1 week – 5 weeks
- 1 month – 11 months
- 1 year – infinity years
Working with Time
When working with dates and times in PHP, as well as any other server side language it’s easiest to perform mathematical calculations using the date/time’s Unix time stamp (the number of seconds since Unix Epoch – Jan 1, 1970.)
To convert to a relative time you’d take the current time stamp using the time() function and subtract the relative date’s time stamp. You’d then use the difference and see where, in the relative range, it lands.
Calculating Time using the Difference
Here’s the function, I created to calculate the relative time, and added it to our theme’s functions.php file. I created to calculate the relative time:
PHP
<?php
if(!function_exists('how_long_ago')){
function how_long_ago($timestamp){
$difference = time() - $timestamp;
if($difference >= 60*60*24*365){ // if more than a year ago
$int = intval($difference / (60*60*24*365));
$s = ($int > 1) ? 's' : '';
$r = $int . ' year' . $s . ' ago';
} elseif($difference >= 60*60*24*7*5){ // if more than five weeks ago
$int = intval($difference / (60*60*24*30));
$s = ($int > 1) ? 's' : '';
$r = $int . ' month' . $s . ' ago';
} elseif($difference >= 60*60*24*7){ // if more than a week ago
$int = intval($difference / (60*60*24*7));
$s = ($int > 1) ? 's' : '';
$r = $int . ' week' . $s . ' ago';
} elseif($difference >= 60*60*24){ // if more than a day ago
$int = intval($difference / (60*60*24));
$s = ($int > 1) ? 's' : '';
$r = $int . ' day' . $s . ' ago';
} elseif($difference >= 60*60){ // if more than an hour ago
$int = intval($difference / (60*60));
$s = ($int > 1) ? 's' : '';
$r = $int . ' hour' . $s . ' ago';
} elseif($difference >= 60){ // if more than a minute ago
$int = intval($difference / (60));
$s = ($int > 1) ? 's' : '';
$r = $int . ' minute' . $s . ' ago';
} else { // if less than a minute ago
$r = 'moments ago';
}
return $r;
}
}
?>
For me that was the easy part. It’s just math, and math is not only fun but very easy for me to work with. WordPress’s tags though, can sometimes be a little bit trickier.
WordPress, Working with Times
I started off with the comments and couldn’t find anything in WordPress Codex about comment date/time functions that would return a value for time instead of printing it to the screen, which would be critical for passing the number through the how_long_ago() function we just created.
So I went poking around in the WordPress core files and scripts and fount the get_comment_time() function, which is built into WordPress and returns a value using the PHP date string passed. So in place of the date rendering in my new template for the comments I used:
PHP
<?php if(!function_exists('how_long_ago')){comment_date() . ' at ' . comment_time(); } else { echo how_long_ago(get_comment_time('U')); } ?>
This will print the relative measurement if we do have the how_long_ago() function in out functions.php file, and it will print the susal date if for sme reason we have forgotten to add the function to our files.
For entries you can use get_the_time() function, like so:
PHP
<?php if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); } ?>
Pretty easy, and a great way to change up your theme, and possibly make it more relevant since people understand and usually think in relative terms.
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)
{ 1 trackback }
{ 12 comments… read them below or add one }
I was trying this on my redesign and for some reason all the $difference’s ware negative until I changed them to: $difference = $timestamp – time(); And now it’s working correctly. Seems strange to me too, but it does…
Forget my last comment, it wasn’t that :) Took me long enough to figure out but it was negative because in “
$difference = time() - $timestamp;” thetime()gets the server timestamp and the$timestamp(which isget_comment_time('U')), is the WordPress time which includes the offset set in the options page. From what they say on http://codex.wordpress.org/Function_Reference/current_time , usingcurrent_time('timestamp')instead oftime()should work but it didn’t… I still had to add my time zone offset in the code.Nice coding – yes it’s good to personalise the timestamps according to the visitor (i.e. when they are reading the comments). I see digg.com does this now too.
This function is great. Thanks!
There is a built-in WordPress function that does something similar, but it’s less elegant:
echo humantimediff(getthetime('U'), time()) . " ago";It doesn’t calculate weeks, apparently, so you get things like “14 days ago.” I like yours much better!
Thanks Stephanie, for simple tasks like this I’d often rather use a function in the
functions.phpfile since this is a great function to use in any other PHP code as well. I am a fan of portable code.It didn’t work for me, the time just got stuck at “moments ago” and it’s been at least 40 mins ago since I last posted it. Anyone has this issue too? How do I get around it? Thanks!
Great script, thanks!
How should I modifiy it to count seconds as well? I would like to display secons if the post isn’t at least one minutes old instead of “moments ago”.
Terri, thanks for the great codes. I appreciated it!
I Terri, I really like your function! I’m not an expert in PHP, if I want write “hour” and “hours” in italian (“ora” and “ore”), how i can do it? Thanks
Wahey thanks for that!
I like it a lot! I could never see the point on some of my blogs why I would need the exact date and time!
Thanks again
Yvonne
Thanks for the code. Elegant and totally interoperable solution. I avoid installing plug-ins like the plague! Rather just do a call and off I go ;)
Great contributions dude!