Skip to content

Instantly share code, notes, and snippets.

@ridvanbaluyos
Last active December 6, 2015 17:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridvanbaluyos/3a0f0f48fe33d9a0e6b3 to your computer and use it in GitHub Desktop.
Save ridvanbaluyos/3a0f0f48fe33d9a0e6b3 to your computer and use it in GitHub Desktop.
<?php
$datetime = '2015-12-07 00:58:18';
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = [
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
];
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment