The last “Time Ago” function is short of few things, how about facebook style “Time ago” for posts and comments in wordpress. The last function that I wrote didn’t calculate for years, for example if I like to display “2 years ago” for a post published 2 years ago, it displayed “730 days ago”, which I didn’t like. So I have come up with a new function which will work for the following – Seconds, Minutes, Hours, Weeks, Months and Years.

open your functions.php and paste this code, a complete function that will return the “time ago” output .

function timeago()
   global $post;

   $unix_date = get_post_time('U', true, $post);

   $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
   $lengths = array("60","60","24","7","4.35","12","10");

   $now             = time();
   // check validity of date
   if(empty($unix_date)) {   
       return "Bad date";

   // is it future date or past date
   if($now > $unix_date) {   
        $difference     = $now - $unix_date;
        $tense         = "ago";

   } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";

   for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];

   $difference = round($difference);

   if($difference != 1) {
        $periods[$j].= "s";

   return "$difference $periods[$j] {$tense}";

Now save your functions.php and wherever you want to display time ago just add this line

<?php echo timeago(); ?>

You can insert into category.php, single.php to display the time elapsed since the post was published. This function right now works for posts alone, soon I will publish with time elapsed display for comments too.

Facebook style “Time Ago” for WordPress
Tagged on:                     


3D modeler and Web designer. Founder of Techlister. Love to build sites using Wordpress and Joomla. Interested in Astronomy.

5 thoughts on “Facebook style “Time Ago” for WordPress

  • Mar 12, 2013 at 10:08 pm

    Actually, people keep calling this “Facebook style Time Ago”, but it isn’t! You never see Facebook say “53 seconds ago”, or “4 weeks ago”, it uses “about a minute ago” for anything over a few seconds but under a minute, and “about a month ago” for anything over about 3 weeks. This is a much more simplistic time elapsed function, NOT like Facebook.

    • Mar 13, 2013 at 5:42 am

      Thanks Nieil, Your comment itself answers why this article is titled so.

  • Apr 2, 2013 at 11:36 am

    Hello Pelister,
    I also want the same feature for Joomla site. Actually I want to display the “…… Time Ago” for my articles on my site instead of Publish date and time. Can you please provide the Joomla version of this coding? and how n where to apply in Joomla?


      • Apr 3, 2013 at 12:11 pm

        Thanks a lot I will wait and keep watching this space.


Leave a Reply

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