add more indicators and a new function to return relative time in the past and the future

This commit is contained in:
Mario
2024-12-07 20:41:13 +00:00
parent d37d181d94
commit e88ae54bef
5 changed files with 60 additions and 14 deletions

View File

@@ -268,6 +268,40 @@ function relative_date($posted_date, $format = null) {
return $abs;
}
function relative_time($timestamp) {
$timestamp = datetime_convert('UTC', date_default_timezone_get(), $timestamp);
$now = new DateTime();
$time = new DateTime($timestamp);
$interval = $now->diff($time);
$prefix = '';
$appendix = ' ' . t('ago');
if ($time > $now) {
$prefix = t('in') . ' ';
$appendix = '';
}
if ($interval->y > 0) {
return $prefix . $interval->y . ' ' . plural_dates('y', $interval->y) . $appendix;
} elseif ($interval->m > 0) {
return $prefix . $interval->m . ' ' . plural_dates('m', $interval->m) . $appendix;
} elseif ($interval->d > 0) {
return $prefix . $interval->d . ' ' . plural_dates('d', $interval->d) . $appendix;
} elseif ($interval->h > 0) {
return $prefix . $interval->h . ' ' . plural_dates('h', $interval->h) . $appendix;
} elseif ($interval->i > 0) {
return $prefix . $interval->i . ' ' . plural_dates('i', $interval->i) . $appendix;
} elseif ($interval->s > 0) {
return $prefix . $interval->s . ' ' . plural_dates('s', $interval->s) . $appendix;
} else {
return t('now');
}
}
function plural_dates($k,$n) {
switch($k) {