Added new function for Relative Dates

This commit is contained in:
chrislemonier 2014-02-25 12:08:58 -08:00
parent e9898ba6bb
commit a32b1da684

View File

@ -77,6 +77,30 @@ function in_list($id, array $listing)
return '?';
}
function relative_time($date, $postfix = ' ago', $fallback = '%e %B %Y %k:%M')
{
$diff = time() - strtotime($date);
if($diff < 60)
return $diff . ' second'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/60);
if($diff < 60)
return $diff . ' minute'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/60);
if($diff < 24)
return $diff . ' hour'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/24);
if($diff < 7)
return $diff . ' day'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/7);
if($diff < 4)
return $diff . ' week'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/4);
if($diff < 12)
return $diff . ' month'. ($diff != 1 ? 's' : '') . $postfix;
return date($fallback, strtotime($date));
}
function error_class(array $errors, $name)
{
return ! isset($errors[$name]) ? '' : ' form-error';