Post Pic

Display dates as “time ago”, the easy way

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

Jul 05 2010 12:40

thanks for this tips, but what about comments?

Jul 05 2010 13:38

@Abdelhadi: This should work:
echo human_time_diff(get_comment_time(‘U’), current_time(‘timestamp’)) . ‘ ago’;

Jul 05 2010 13:42

Excellent wee peice of code to help people, thanks for this, will store it for future use.

Jul 05 2010 19:57

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?

Jul 05 2010 20:39

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!

Jul 05 2010 21:21

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

Jul 05 2010 22:26

Thanks for the code for comments, it’ll be better to include it in the post, I think :)
Thanks again.

Jul 06 2010 02:42

Ohh superb….!!!!

You are awesome friend, thanks a lot.

Jul 08 2010 09:01

this is awesome
and we can add this to anywhere in wordpress right?

Jul 08 2010 10:51

Useful snippet as always, thanks !

Jul 11 2010 09:39

Simple snippet code but powerful. Thanks for sharing this to us :)

Jul 13 2010 12:07

I prefer like this, thanks for the snippet ;)

Jul 22 2010 05:27

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?

Jul 27 2010 01:49

I like this- Thanx!

Jul 31 2010 13:28

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.

Aug 12 2010 23:23

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”?

Aug 13 2010 13:14

Hi, Thanks for the code. You just saved my time.

Aug 15 2010 22:40

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?

Aug 19 2010 23:36

Thanks for the code :)

Aug 22 2010 16:26

Is there a way to internationalize this trick?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required