Display “Time Ago” for Posts and Comments in WordPress

Written by pelister. Posted in Wordpress

Most of the templates for WordPress displays author info, tags, date and sometimes time also in every posts. If you had ever thought of displaying time in facebook style, here is a solution for you.

Open functions.php, from the themes folder, fot the theme you wish to use this feature and add these lines.

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

then save your functions.php. This function will return the time ago the post was published. Add the following line wherever you need to display time ago.

<?php echo timeago(); ?>

You can add this to your single.php – usually for single posts or comments.php – for comments. Usage depends on your template design and the area of need.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
pelister
3D modeler and Web designer. Founder of Techlister. Love to build sites using WordPress and Joomla. Interested in Astronomy, project member of SETI (Search for Extra Terrestrial Intelligence).

Tags: , , ,

Comments (2)

  • Alex

    |

    Hello there,
    It is possible to add the word “ago” in front of the current time?
    I have my WP in romanian language and it says “17 ore ago” ,which does not look good, should be “acum 17 ore” (acum means ago).

    Reply

    • pelister

      |

      replace this line
      return human_time_diff($d(‘U’), current_time(‘timestamp’)) . ” ” . __(‘ago’);

      with this

      return “acum “. human_time_diff($d(‘U’), current_time(‘timestamp’)) .” ore”;

      Reply

Leave a comment

Techlister© - 2012