How to display Twitter-like “time ago” on your WordPress blog!

twitterIf you use micro-blogging services like Twitter then you may be aware of the way they show how long ago the status was updated, So, how about integrating the same feature in your WordPress blog and replace that old boring time stamp to this new “Time Ago”. There are some ways to display Twitter-like “time ago” date format in our WordPress. We can use plugin or we can create and write little functions to handle this.

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“…

Twitter have a really cool built-in function that display time from now, like “3 days ago” or “more than a month ago”. Doing so with WordPress is not hard using some PHP.

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. The first thing to do is to create the function. To display Twitter-like “time ago”, paste the following into your functions.php file:

function time_ago( $type = 'post' ) {
	$d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
	return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
}

Once done, you can use the function in your theme files:

<?php echo time_ago(); ?>

Thanks to UpThemes for this trick!