Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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