Skip to content

Instantly share code, notes, and snippets.

@queued
Forked from xeoncross/gist:6595592
Last active September 5, 2015 15:54
Show Gist options
  • Save queued/59f1bbdca0c8d4d528ad to your computer and use it in GitHub Desktop.
Save queued/59f1bbdca0c8d4d528ad to your computer and use it in GitHub Desktop.
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