Thread  RSS Function: howLongAgo()



# 11536 9 years ago on Sat, Jan 16 2016 at 10:25 pm

Here is another little helpful function that I put together in PHP. It displays how long ago a time stamp is from the current time in denominations of seconds, minutes, hours, days, weeks, etc.

function howLongAgo($datetime){
  
  $diff = time() - strtotime($datetime);
  
  $units = Array(
    'second'  => 1,
    'minute'  => 60,
    'hour'    => 60 * 60,
    'day'     => 60 * 60 * 24,
    'week'    => 60 * 60 * 24 * 7,
    'month'   => 60 * 60 * 24 * 30,
    'year'    => 60 * 60 * 24 * 365  
  );
  $units = array_reverse($units);
  
  foreach($units as $unit => $divisor){
    if($diff > $divisor){
      $p = (round($diff / $divisor) > 1) ? 's' : '';
      return round($diff / $divisor).' '.$unit.$p.' ago';
    }
  }
  
  return 'just now';
  
}

73's, KD8FUD

User Image


Return to Index Return to topic list

Forgot password?
Currently Online
Users:0
Guests:22

Most Recently Online
ZOL3 weeks ago
Wolfwood293 weeks ago
lam3 weeks ago
Nitrocosm1 month ago
Jovian1 month ago