Skip to content

Instantly share code, notes, and snippets.

@Brammm
Created September 16, 2013 12:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Brammm/6580046 to your computer and use it in GitHub Desktop.
Save Brammm/6580046 to your computer and use it in GitHub Desktop.
Time interval function
public function timeAgo(\Datetime $then)
{
$now = new \Datetime('now');
$interval = $then->diff($now);
$points = array(
'y' => array('year', 'years'),
'm' => array('month', 'months'),
'd' => array('day', 'days'),
'h' => array('hour', 'hours'),
'i' => array('minute', 'minutes'),
);
foreach ($points as $key => $point) {
if ($interval->$key >= 1) {
$string = $interval->$key == 1 ? $point[0] : $point[1];
return '<time datetime="'.$then->format('Y-m-d H:i:s').'" title="'.$then->format('Y-m-d H:i:s').'">'.$interval->$key.' '.$string.' ago'.'</time>';
}
}
return 'just now';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment