DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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. Here is a function to do so.

The first thing to do is to create the function. To do so, 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!

Tags

One Comment

  1. This is perfect for displaying how long ago you posted on your blog. Is there a way of displaying how long ago you sent your last tweet? I can display my last tweet’s text but am having trouble with the time ago.

Leave a Comment

Your email address will not be published. Required fields are marked *