A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Code Snippets > PHP > Time Ago Function Submit one!

Time Ago Function

This can be used for comments and other from of communication to tell the time ago instead of the exact time which might not be correct to some one in another time zone.

The function only uses unix time stamp like the result of time();

Technique #1

<?php
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' ";
}
?>

Technique #2

function _ago($tm,$rcs = 0) {
   $cur_tm = time(); $dif = $cur_tm-$tm;
   $pds = array('second','minute','hour','day','week','month','year','decade');
   $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
   for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);

   $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
   if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
   return $x;
}

Needs a time() value, and it will tell you how many seconds/minutes/hours/days/years/decades ago.

Subscribe to The Thread

  1. Kenrick says:

    YAY my time ago function is up !!!!

  2. ThinkSoJoE says:

    In the first technique, you don’t need the single quotes around ‘ago’ in the return line, unless you were to use


    return $difference . $periods[$j] . ' ago';

    Or something along those lines

    • TeMc says:

      Indeed, the ‘ago’ isn’t only redundant it also breaks the flow when used inline like:

      <?php
      echo "It was done about " . ago($specialTime) . "). Byebye.";
      ?>

      Instead simply fix it by using $tense as defined

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

  3. aSeptik says:

    the second function is taken from php.net

    http://www.php.net/manual/en/function.time.php#91864

    and it should be corrected from

    function _ago($tm,$rcs = 0) {

    to

    function time_ago($tm,$rcs = 0) {

    otherwise you’ll get it wrong.

  4. Xcoder says:

    Very useful, thanks alot!

  5. If you make some minor changes, you can specify how many smaller units should be appended to the string (using the 2nd parameter).

    Just replace the 7th line with this:

    if(($rcs > 0)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm, --$rcs);

    Example:

    time_ago(1291809455) will return “1 month”
    time_ago(1291809455, 1) will return “1 month 2 weeks” (this works without changing the original)
    time_ago(1291809455, 2) will return “1 month 2 weeks 4 days”
    etc.

  6. Used this for my forum latest posts, thanks a lot for this worked perfectly!

  7. Thank you for this snippet! It has served me well in a Facebook Application that I am developing currently. I’ve left you guys a nice comment credit in the source code.

  8. Coder :D says:

    nice :( but mine not working :( please help me :(( :DD thank you very much :DD :DD

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---