WordPress Master Tip: Display "Time Ago" for Posts or Comments
- Posted by Chris Wallace on March 24th, 2010 with 24 comments and tagged: human_time_diff, time ago
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”
Leave a Reply
-
Recent Posts
Recent Comments
- Helmuts on We Just Joined the 19th Century! We Have a Facebook Page!
- New Real Estate Theme | WordPress Real Estate Plugin - WordPress Real Estate Themes - WP REALTY on The UpThemes Framework
- 50 Great Free WordPress Themes | MoeMir on The UpThemes Framework
- 50大免费WordPress的主题,从2011年 | 设计人员和开发人员的自由和有用的在线资源网 on The UpThemes Framework
- Quora on Gallery
Archives
Categories
Meta


Nice work!!
Pretty nifty! Will definitely come in handy for my future projects. =)
Thanks Lasha! Long time no chat, friend.
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.
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.
Nice! Been looking for a way to do this with my twitter feed as well. I’ll have to have a play.
[...] “Time Ago” for Posts or Comments [...]
Very nice, very simple, love it!
[...] 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. [...]
Nice function. Thank you for this post.
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.
“Conversate” will be free? I hope yes because it is based on a free theme!
Thank you
Thanks Chris! I just swapped this out for the code I manually wrote…kudos!
This tricks work on comment right?
Yep, it works for posts and comments.
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.
[...] Источник: upthemes.com [...]
[...] to Chris for sharing this [...]
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
[...] upthemes.com [...]
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!