
This small code snippet will help you to display the time ago for WordPress post if your post was published less than 24 hours ago. You can find some code published on other blogs that used for displaying the time different from now to the time when the post published like: “minutes ago“, “hours ago“, “days ago“… This code will display only when the different time is less than 24 hours.
Like the article? Be sure to subscribe to our RSS feed and follow us on Twitter to stay up on recent content.
The completed code you need is below. Copy it and paste it to the functions.php file and you are ready to go. No more messing needed.
add_filter('the_time', 'timeago'); function timeago() { global $post; $date = $post->post_date; $time = get_post_time('G', true, $post); $time_diff = time() - $time; if ( $time_diff > 0 && $time_diff < 24*60*60 ) $display = sprintf( __('%s ago'), human_time_diff( $time ) ); else $display = date(get_option('date_format'), strtotime($date) ); return $display; }
This code will hook into the the_time() function. It will do the filter which modify the return value of the_time() before sending it to the browser screen.
The output:
Published: 5 minutes ago Published: 12 hours ago Or Published: March 14, 2010 //Depends on your date format setting
Don’t forget to subscribe to our rss feed to receive full code snippets and articles via feed reader or follow us on twitter.
Advertisement
You May Also Like
Related Articles


Thanks for the tip. I’ll definitely use this while doing my next wordpress redesign!
A great little snippet. Thanks!
.-= HD Guy´s last blog ..LG LX9500 – World’s First Full LED 3D TV =-.
Thanks for the little information
.-= SmashingWebs.com´s last blog ..Top RSS icon packs for your blog =-.
Such a useful info, I gonna use it straight away.
Nice tip. But can you tell me what the benefit is?
.-= Patrick Matsumura´s last blog ..Warum mobile Internetseiten immer wichtiger werden… =-.
No messing code needed for theme development. You usually use < ?php echo timeago(); ?> wherever you want to display it, right? No, this one will hook into the default the_time() function.
Thanks Lam, great snippet!, and easy to implement!
.-= Franco´s last blog ..Giveaway: Greeting Cards from UPrinting.com†=-.
A plugin could solve the problem of users having to mess around with their theme code. Hey Lam, how about we build a plugin for this together? Interessted?
nice tip Lam, thank you very much!
.-= Nikola Lazarevic´s last blog ..New and Updated WordPress Plugins in 2010 =-.
Cool, short quick tips at it’s best!
.-= Aidan´s last blog ..Onextrapixel Turns One with A New Look and Presents for You! =-.
This is useful for what and how do I install
.-= tech never´s last blog ..headlamps for miners =-.
Useful: put it in your functions.php file and that’s it.
How: put it in your functions.php file and that’s it.
perfect tweaks, let me cultivate my codes again
.-= Mars´s last blog ..17 Apple Store Cube Photos =-.
Good stuff Lam, definitely going to use this. Thanks!
Hi! Thanks!
But, I’m French, and the time is in English now with your tip… How to show the time as j F Y ?
Cheers!
i was exactly looking for this, thanks alot
what about the comment’s time ?? what should i do to show comments time in hours ?
i.e get_comment_date
good trick, check out http://www.scriptplazza.com/display-timeago-in-wordpress/
Thanks Lam. I have searching for this code on the net to implement on my site’s homapage. It really helps and improve my homepage.
Just the snippet what I was looking for, now I’ve just got to try get it to work
I’m looking to highlight all the posts under a week old, so this should be fine! Thanks a lot!