
Want to display human readable dates on your blog, such as “Posted 6 days ago?” There’s a lot of available functions to do so, but only a few people know that WordPress have its own built-in function to do that.

Want to display human readable dates on your blog, such as “Posted 6 days ago?” There’s a lot of available functions to do so, but only a few people know that WordPress have its own built-in function to do that.
To display human readable dates on your blog, you have to use the human_time_diff() function. The following piece of code will show a post date like "Posted 6 days ago".
Paste it anywhere within the loop, save the file, and you're done.
Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>
Credits: PHP Snippets.
24 Responses
thanks for this tips, but what about comments?
@Abdelhadi: This should work:
echo human_time_diff(get_comment_time(‘U’), current_time(‘timestamp’)) . ‘ ago’;
Excellent wee peice of code to help people, thanks for this, will store it for future use.
What if one wants to have it in other languages than English?
I see that it’s easier to use the code from PHPsnippets, but it is possible to extend the WP feature with custom text or other language?
Great little snippet… It’s true what they say, you learn somehing new every day
One thing I would suggest is to not use this exclusively, but to only display it in this format is the post/comment is in the last X days. Otherwise it just gets stupid when you see a post was published 1234 days ago!
Good tips! At this time I’m using the Timeago plugin to do the same on my blog. Perhaps using the native wp function could be better.
Thanks for sharing
Thanks for the code for comments, it’ll be better to include it in the post, I think
Thanks again.
Ohh superb….!!!!
You are awesome friend, thanks a lot.
this is awesome
and we can add this to anywhere in wordpress right?
Useful snippet as always, thanks !
Simple snippet code but powerful. Thanks for sharing this to us
I prefer like this, thanks for the snippet
Better than DD-MM-YYYY kind of format. I am wondering if i can show today’s date in post or pages? Is that possible?
I like this- Thanx!
This function is very useful.By using this function we can see when we last posted on any Blog We can also see that when was the last activity in any Blog Post.
Anybody know how to set a time condition on this?
So anything over a week or so would just show the date it was posted instead of something useless like “423 days ago”?
Hi, Thanks for the code. You just saved my time.
I’d like to be able to display a global “Site last updated on : ” notice, as “time ago”. I’m using the following to retrieve the most recently modified post/page and display it’s timestamp :
global $wpdb;
$last = $wpdb->get_var(“SELECT post_modified FROM $wpdb->posts order by post_modified DESC LIMIT 1″);
echo “Site last updated: ” . mysql2date((‘D j/n/y \a\t g:ia’), $last);
I’ve been trying to convert it to display “time ago” with human_time_diff, but am completely stuck. Do you have any suggestion as to how I might go about it?
Thanks for the code
Is there a way to internationalize this trick?
Trackbacks: