Skip to content

Instantly share code, notes, and snippets.

@Zorono
Last active August 29, 2018 23:10
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 Zorono/07c9cd0bcce678741ec822561d024aab to your computer and use it in GitHub Desktop.
Save Zorono/07c9cd0bcce678741ec822561d024aab to your computer and use it in GitHub Desktop.
<?php
function ReturnTimeLapse($start, $till)
{
$time = array(
'second' => $till - $start,
'minute' => 60,
'hour' => 60*60,
'day' => 60*60*24,
'month' => 60*60*24*30
);
switch($time['second'])
{
case ($time['second'] == 1): {
$result = 'a seconds ago';
}
break;
case ($time['second'] < (1 * $time['minute'])): {
$result = (is_float($time['second']) ? floor($time['second']) : $time['second']). ' seconds ago';
}
break;
case ($time['second'] < (2 * $time['minute'])): {
$result = 'a minute ago';
}
break;
case ($time['second'] < (45 * $time['minute'])): {
$result = (is_float($time['second'] / $time['minute']) ? floor($time['second'] / $time['minute']): $time['second'] / $time['minute']). ' minutes ago';
}
break;
case ($time['second'] < (90 * $time['minute'])): {
$result = 'a hour ago';
}
break;
case ($time['second'] < (24 * $time['hour'])): {
$result = (is_float($time['second'] / $time['hour']) ? floor($time['second'] / $time['hour']) : $time['second'] / $time['hour']). ' hours ago';
}
break;
case ($time['second'] < (48 * $time['hour'])): {
$result = 'Yesterday';
}
break;
case ($time['second'] < (30 * $time['day'])): {
$result = (is_float($time['second'] / $time['day']) ? floor($time['second'] / $time['day']) : $time['second'] / $time['day']). ' days ago';
}
break;
case ($time['second'] < (12 * $time['month'])): {
$rounded_month = round($time['second'] / $time['day'] / 30);
$result = ($rounded_month <= 1 ? 'One month ago' : $rounded_month. ' months ago');
}
break;
case ($time['second'] > (12 * $time['month'])): {
$rounded_year = round($time['second'] / $time['day'] / 365);
$result = ($rounded_year <= 1 ? 'One year ago' : $rounded_year. ' years ago');
}
break;
}
return $result;
}
echo ReturnTimeLapse(1535322044, time());
/*
Output: 4 minutes ago
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment