Simple, free resources to help you build a better WordPress website.

Sign up for our newsletter.

WordPress Master Tip: Display "Time Ago" for Posts or Comments

While working on the upcoming release of Conversate, our revamped version of the now-famous P2 Theme by Automattic, I rewrote one of the functions they used to display the time on each post, which looked rather ugly on the P2 theme. They used the time and date for each post and with a microblogging platform, it’s typically best to show how long ago something was posted. See example below:

So I thought I would share the function to do this:

function time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';

    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');

}

It uses the built-in WordPress function human_time_diff(), which is pretty rad if you ask me. To use it, simply place the function call time_ago() in your single.php or custom comment callback function. Example:

<?php echo time_ago(); ?>

29 Comments

    • Avatar
      Chris says:

      Thanks Matt. One thing that I think can be changed is after a certain amount of time you show the actual time and date, because nobody wants to see “posted 124 days ago” on the page. Also, we could add some microformatting in as well.

  1. Avatar
    Matt Haltom says:

    I’ve seen this done before but interesting to see the code behind it. Personally I would never use it though because I just don’t agree with the approach. I think people are smart enough to read a date and figure out how long ago it was.

    When it comes to articles some people want to cite other article sources via MLA or whatever format you are using. When you convert the date to how long ago the post was, it becomes hard to pull out the information easily to correctly cite that article or post.

    However there is always an argument to use one or the other right? 🙂

    • Avatar
      Chris says:

      Definitely. 😉

      However, I would argue that when you’re reading status updates or even comments, sometimes it’s better to quickly see how long ago it was posted. Certainly, it’s not the right solution for the typical blog.

  2. Pingback: Things I Found Interesting Around March 25th | Chris Coyier

  3. Pingback: Trabajando con fechas relativas en Wordpress | Eliseos.net

  4. Pingback: Displaying What Time Ago A Post or Comment Is Published In WordPress

  5. Avatar
    Jim Munro says:

    Cool post, I’ve added this elsewhere but for some reason never thought to do it on my own blog. I definitely want to try it now.

    I would partially agree with the comment on readability. I would suggest adding a line or two in the code to see if it’s greater than x days to start converting it back to the mmddyy format for readability as 6 weeks ago isn’t all that useful for most people.

    Also, you might want to only do this on the front page of your blog and in single posts display normal dates.

    Just some ideas. Thanks for posting.

  6. Avatar
    TradiArt says:

    This is extremely useful in WP sites with frequent updates and lots of comments. Sometimes it is better to see how much time has passed from the moment the post was published. Will be useful in some of our projects. Thank you.

  7. Pingback: Как отображать «X минут назад» а-ля Twitter в вашем блоге? | SEO сканер

  8. Pingback: How to Display “Time Ago” for Posts and Comments « Private Gardens

  9. Avatar
    Simon says:

    Hey Chris, I’ve saved this tut for later use which is now. I would like to know though how I could do exactly what you mentioned a bit earlier: After 14 days, it should swith to the date it was posted. Any help on this? Would be appreciated 🙂

  10. Pingback: GRiFblog.com » Blog Archive » «Косим» под Twitter

  11. Avatar
    Toure says:

    Interesting, but I don’t wanna see “2 years ago”. How about limiting this to a max of 24 hours and everything else stays on default time stamp?

Leave a Reply

Your email address will not be published. Required fields are marked *