Relative Dates in WordPress Templates


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.

Popularity: 51% [?]


Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!