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(); ?>

24 Responses to “WordPress Master Tip: Display "Time Ago" for Posts or Comments”

  1. Pretty nifty! Will definitely come in handy for my future projects. =)

  2. Matthew Simo says:

    Very nice Chris, this might be the best implementation of this type of functionality I’ve seen. Kudos!

    • 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.

  3. 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? :)

    • 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.

  4. Tom says:

    Nice! Been looking for a way to do this with my twitter feed as well. I’ll have to have a play.

  5. Will says:

    Very nice, very simple, love it!

  6. [...] y difíciles de personalizar o requerían una conexión extra a nuestra base de datos. Hoy en Upthemes.com presentaban una función que se acercaba bastante a lo que tenia en mi cabeza. Encontré dos formas [...]

  7. Rilwis says:

    Nice function. Thank you for this post.

  8. 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.

  9. Alberto says:

    “Conversate” will be free? I hope yes because it is based on a free theme!
    Thank you

  10. Rogie King says:

    Thanks Chris! I just swapped this out for the code I manually wrote…kudos!

  11. Jauhari says:

    This tricks work on comment right?

  12. 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.

  13. 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 :)

  14. I guess I’m a little late to the party here, just finding this now! Kudos Chris! I’ll be using this on a client project currently in development. Cheers!



Leave a Reply