Display dates as “time ago”

Many popular social networking sites such as Digg or Twitter don’t display dates like Published on June 25. Instead, they display Published 10 days ago.
Here’s a PHP function to convert a date into a “time ago” date.

function  timeAgo($timestamp, $granularity=2, $format='Y-m-d H:i:s'){
        $difference = time() - $timestamp;
        if($difference < 0) return '0 seconds ago';
        elseif($difference < 864000){
                $periods = array('week' => 604800,'day' => 86400,'hr' => 3600,'min' => 60,'sec' => 1);
                $output = '';
                foreach($periods as $key => $value){
                        if($difference >= $value){
                                $time = round($difference / $value);
                                $difference %= $value;
                                $output .= ($output ? ' ' : '').$time.' ';
                                $output .= (($time > 1 && $key == 'day') ? $key.'s' : $key);
                                $granularity--;
                        }
                        if($granularity == 0) break;
                }
                return ($output ? $output : '0 seconds').' ago';
        }
        else return date($format, $timestamp);
}

Usage

$time = timeAgo($dateRef);

The function takes 3 parameters:

  • timestamp: Your date
  • granularity: How far you go in the precision of your date. Default is 2 (1 day, 15 hours ago)
  • format: Desired format for the date. Default is Y-m-d H:i:s.

Using the timeAgo() function within WordPress

Using timeAgo() in WordPress is not hard at all. Use the following code within WordPress loop to display the “time ago” date of a post:

echo timeAgo(get_the_time('U'));

3 Comments

  1. Gerhard
    Posted June 25, 2010 at 2:15 pm | Permalink

    Nice snippet, thanks!

  2. Posted July 4, 2010 at 2:31 pm | Permalink

    WordPress already has the human_time_diff() function.

    [?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?]

    More in the Codex
    http://codex.wordpress.org/Function_Reference/human_time_diff

  3. Posted July 14, 2010 at 2:31 pm | Permalink

    Thanks for the tips, it’s better like this!

5 Trackbacks

  1. [...] This post was mentioned on Twitter by Jean-Baptiste Jung and Pierpaolo Frigerio, PHP Snippets. PHP Snippets said: PHP Snippet: Display dates as “time ago” http://bit.ly/bX9ddY #php [...]

  2. By Display dates as “time ago”, the easy way on July 5, 2010 at 6:28 am

    [...] PHP Snippets. If you enjoyed this article, please consider sharing it! tweetmeme_style = 'compact'; [...]

  3. [...] Credits: PHP Snippets. [...]

  4. [...] Snippets has a copy-and-paste-able function that works in much the same way. It takes a UNIX timestamp as an input and uses the [...]

  5. [...] Credits: PHP Snippets. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>