Thread  RSS Function: howLongAgo()



# 11536 8 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:21

Most Recently Online
Nitrocosm2 months ago
Lexica4 months ago
Wolfwood294 months ago
Miroku5 months ago
Doitsujin5 months ago