34

Can anyone tell me how I can display a status message like "12 seconds ago" or "5 minutes ago" etc in a web page?

Nitesh
  • 2,286
  • 2
  • 43
  • 65
Niyaz
  • 53,943
  • 55
  • 151
  • 182
  • This question was [previously asked](http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time), the example code in answers should be pretty easy to convert to PHP. – Wedge Aug 20 '08 at 20:07
  • 1
    This has been covered (though with more of a C# focus) in [this thread](http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time). – Ian Nelson Aug 20 '08 at 20:05
  • There is a nice jquery plugin : timeago.js – Loïc MICHEL Apr 27 '13 at 04:46

3 Answers3

73

Here is the php code for the same:

function time_since($since) {
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
        array(1 , 'second')
    );

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    return $print;
}

The function takes the number of seconds as input and outputs text such as:

  • 10 seconds
  • 1 minute

etc

Niyaz
  • 53,943
  • 55
  • 151
  • 182
  • Oh and don't forget to change those multiplications with the real values, so that it won't be calculated every time it runs :) – AntonioCS Aug 23 '09 at 21:07
  • 10
    Since I was curious, replacing the multiplication sequences with the evaluated products was ~1.2% faster. – Mike B Aug 24 '09 at 01:04
  • 5
    It only shows `33 minutes` no matter what I changing the date and time too – Airikr Feb 24 '13 at 18:18
  • 7
    @ErikEdgren I had same problem because I was sending date instead of seconds. Try this: `time_since(time() - strtotime($datetime))` – Igor Jerosimić Aug 02 '13 at 15:12
  • TWEAK WITH JUST NOW. function time_since($since) { $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'), array(60 * 60 * 24 * 7, 'week'),array(60 * 60 * 24 , 'day'),array(60 * 60 , 'hour'),array(60 , 'minute'),array(1, 'sec')); for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; $count = floor($since / $seconds); if ($count != 0) {break;} } if( $count == 1){ return $print = '1 '.$name;} else if($count < 1) { return $print = 'just now'; } else { return $print = "$count {$name}s" ;} } – Crisam De gracia Jan 31 '18 at 23:26
16
function timeAgo($timestamp){
    $datetime1=new DateTime("now");
    $datetime2=date_create($timestamp);
    $diff=date_diff($datetime1, $datetime2);
    $timemsg='';
    if($diff->y > 0){
        $timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');

    }
    else if($diff->m > 0){
     $timemsg = $diff->m . ' month'. ($diff->m > 1?"'s":'');
    }
    else if($diff->d > 0){
     $timemsg = $diff->d .' day'. ($diff->d > 1?"'s":'');
    }
    else if($diff->h > 0){
     $timemsg = $diff->h .' hour'.($diff->h > 1 ? "'s":'');
    }
    else if($diff->i > 0){
     $timemsg = $diff->i .' minute'. ($diff->i > 1?"'s":'');
    }
    else if($diff->s > 0){
     $timemsg = $diff->s .' second'. ($diff->s > 1?"'s":'');
    }

$timemsg = $timemsg.' ago';
return $timemsg;
}
Dipesh
  • 379
  • 3
  • 10
  • 2
    I used a modified version of this, I replaced the date_create line with $datetime2 = new DateTime(); $datetime2->setTimestamp($timestamp); and removed all the apostrophes before the s string – Oliverb Aug 02 '16 at 09:30
6

PHP's \DateTime::diff returns a \DateInterval object on which you can get the minutes by the public i property.

A J
  • 3,970
  • 14
  • 38
  • 53