Display Time Since Published in WordPress
by c.bavota | Posted in Tutorials | 20 comments
May 01 2012
This is just a little snippet I discovered in the WordPress codex that allows your to display the time since your post was published. I wrote it into a little function that you can add to your functions.php file:
/**
* Display time since post was published
*
* @uses human_time_diff() Return time difference in easy to read format
* @uses get_the_time() Get the time the post was published
* @uses current_time() Get the current time
*
* @return string Timestamp since post was published
*
* @author c.bavota
*/
function get_time_since_posted() {
$time_since_posted = human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago';
return $time_since_posted;
}
Now you just need to include get_time_since_posted() in the WordPress loop to display the time since your post was published.
Read more about human_time_diff() at http://codex.wordpress.org/Function_Reference/human_time_diff.



Has my last thank you message been received? The page refreshed but no messages are showing on your blog.
I was just saying thank you Bavota for the time function, hope it works this time..
Ben
it’s great ! i had to write a long , very long function to do this thing ! but now it’s simple, so simple , what amazing !
Hi there, I like the idea – nice little novelty to add to your page. Sorry to ask a noob question though, but let’s say you update an old blog (perhaps a piece of research that has remained popular and relevant) – will the date refresh to the last time the page was saved?
No. It will still be based off of the published time. You could change that by using
get_the_modified_date()instead ofget_the_time().Thanks for the reply c.bavota – that’s actually a good thing as far as I’m concerned. Cheers!
Does this code go into the child themes functions.php file or can it be pasted into a hook plugin like Genesis simple hooks?
It could go into your parent theme’s functions.php file or the child theme. Up to you.
Does the function get_the_time() you mentioned above any different from the function the_time() which is present in default wordpress themes and located in single.php
One echoes the time the other just returns it.
Would you know how to display this code for the comments left in a post? Specifically the time a comment was posted or last edited?
it’s great ! i had to write a long , very long function to do this thing ! but now it’s simple, so simple , what amazing !
it’s great !
greeting