Have you fantasized of showing twitter like time on your blog, like posted 2 days ago ? I am sure after twitter showing this kind of time every web application wanted to show the same. WordPress introduced this long back(since v 1.5.0) but is used by very less people in the blogging community. I am going to show you how you can use WordPress function human_time_diff function to show the human readable time.

This is a very simple WordPress function which takes two arguments and returns the human readable time such as “1 hour”, “5 mins”, “2 days”. You might have noticed that I implemented the same on Digimantra few days back. I have used the following function –

// human time in wordpress
function human_time($time){
echo human_time_diff($time,current_time('timestamp'))." ago";
}

To show the time simply replace the old function the_time() with the following function and you should be good.

human_time(get_the_time('U'));

Notice that I have used get_the_time() instead of the_time() since as a standard in WordPress the functions with get_ prefix return values and without get echo the value of the function. I am assuming that your theme has used the_time() function since this is the standard while developing themes in WordPress.

Hope this nifty little code snippet helps you. If you have any questions, do share!

Stay Digified!!
Sachin Khosla

Share this post: