| 1 date.inc | date_format_interval($date, $granularity = 2, $display_ago = TRUE) |
Formats a time interval with granularity, including past and future context.
Parameters
object $date: The current date object.
int $granularity: (optional) Number of units to display in the string. Defaults to 2.
Return value
string: A translated string representation of the interval.
See also
File
- core/
includes/ date.inc, line 723 - Date API functions.
Code
function date_format_interval($date, $granularity = 2, $display_ago = TRUE) {
// If no date is sent, then return nothing.
if (empty($date)) {
return NULL;
}
$interval = REQUEST_TIME - $date->format('U');
$format_interval = format_time_diff(REQUEST_TIME, $date->format('U'), array(
'granularity' => $granularity,
'strict' => FALSE,
));
if ($interval > 0) {
return $display_ago ? t('!time ago', array(
'!time' => $format_interval,
)) : t('!time', array(
'!time' => $format_interval,
));
}
else {
return $format_interval;
}
}