Skip to content

Instantly share code, notes, and snippets.

@samsoir
Created June 15, 2009 18:40
Show Gist options
  • Select an option

  • Save samsoir/130261 to your computer and use it in GitHub Desktop.

Select an option

Save samsoir/130261 to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') or die('No direct script access.');
class chrono_Core {
/**
* Outputs a textual representation of
* how long ago something happened
*
* @param int time_stamp
* @return string
* @author Sam Clark
*/
public static function age($time_stamp)
{
// Current time
$current_time = time();
// Create time array
$time = array
(
'seconds' => $current_time - $time_stamp,
'minutes' => floor(($current_time - $time_stamp) / 60),
'hours' => floor(($current_time - $time_stamp) / (60 * 60)),
'days' => floor(($current_time - $time_stamp) / (60 * 60 * 24)),
'weeks' => floor(($current_time - $time_stamp) / (60 * 60 * 24 * 7)),
'years' => floor(($current_time - $time_stamp) / (60 * 60 * 24 * 365)),
);
if ($time['years'] > 0)
return $time['years'].' year'.($time['years'] == 1 ? '' : 's').' ago';
else if ($time['weeks'] > 0)
return $time['weeks'].' week'.($time['weeks'] == 1 ? '' : 's').' ago';
else if ($time['days'] > 0)
return $time['days'].' day'.($time['days'] == 1 ? '' : 's').' ago';
else if ($time['hours'] > 0)
return $time['hours'].' hour'.($time['hours'] == 1 ? '' : 's').' ago';
else if ($time['minutes'] > 0)
return $time['minutes'].' minute'.($time['minutes'] == 1 ? '' : 's').' ago';
else if ($time['seconds'] > 0)
return $time['seconds'].' second'.($time['seconds'] == 1 ? '' : 's').' ago';
}
} // End Chrono helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment