Return to Snippet

Revision: 43782
at March 30, 2011 23:47 by mike_fowler


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

   $now = time();

       $difference     = $now - $time;
       $tense         = "ago";

   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] 'ago' ";
}

Initial URL
http://css-tricks.com/snippets/php/time-ago-function/

Initial Description
Accepts a UNIX timestamp, like the result of time(), and formats it into "# (days/months/etc) ago"

Initial Title
"Time Ago" Calculation

Initial Tags

                                

Initial Language
PHP