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(); ?>
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.
Pingback: Things I Found Interesting Around March 25th | Chris Coyier
Very nice, very simple, love it!
Pingback: Trabajando con fechas relativas en Wordpress | Eliseos.net
Pingback: Displaying What Time Ago A Post or Comment Is Published In WordPress
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.
Pingback: Как отображать «X минут назад» а-ля Twitter в вашем блоге? | SEO сканер
Pingback: How to Display “Time Ago” for Posts and Comments « Private Gardens
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 🙂
Pingback: GRiFblog.com » Blog Archive » «Косим» под Twitter
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!
Thanks man, super useful!
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?
You’ve one of the better websites
Thanks. Helped me.
Thanks Nice work!!