Skip to content

Instantly share code, notes, and snippets.

@taftse
Created August 26, 2011 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taftse/1173418 to your computer and use it in GitHub Desktop.
Save taftse/1173418 to your computer and use it in GitHub Desktop.
time_past
<?php
public function time_past($time, $opt = array()) {
// The default values
$defOptions = array(
'to' => time(),
'parts' => 1,
'precision' => 'minute',
'distance' => TRUE,
'separator' => ', '
);
$opt = array_merge($defOptions, $opt);
// Default to current time if no to point is given
(!$opt['to']) && ($opt['to'] = time());
// Init an empty string
$str = '';
// To or From computation
$diff = ($opt['to'] > $time) ? $opt['to']-$time : $time-$opt['to'];
// An array of label => periods of seconds;
$periods = array(
'decade' => 315569260,
'year' => 31556926,
'month' => 2629744,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
// Round to precision
if ($opt['precision'] != 'second')
$diff = round(($diff/$periods[$opt['precision']])) * $periods[$opt['precision']];
// Report the value is 'less than 1 ' precision period away
(0 == $diff) && ($str = 'less than 1 '.$opt['precision']);
// Loop over each period
foreach ($periods as $label => $value) {
// Stitch together the time difference string
(($x=floor($diff/$value))&&$opt['parts']--) && $str.=($str?$opt['separator']:'').($x.' '.$label.($x>1?'s':''));
// Stop processing if no more parts are going to be reported.
if ($opt['parts'] == 0 || $label == $opt['precision']) break;
// Get ready for the next pass
$diff -= $x*$value;
}
$opt['distance'] && $str.=($str&&$opt['to']>$time)?' ago':' away';
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment