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:
1 2 3 4 5 6 | 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:
1 | <?php echo time_ago(); ?> |
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!
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 :)
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.
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.
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? :)
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.
Very nice Chris, this might be the best implementation of this type of functionality I've seen. Kudos!
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.


[...] “Time Ago” for Posts or Comments [...]
[...] 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 [...]
[...] Thanks to upThemes for this little snippet. [...]
[...] Источник: upthemes.com [...]
[...] to Chris for sharing this [...]
[...] upthemes.com [...]