From 1351be7da8a292396aa7eca46d54cee3240b05cf Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 11 Jul 2019 13:03:04 +0200 Subject: [PATCH] Add param to timeUntil() to require time output If the time specified in timeUntil() is at least 3 days in the future, the output will only contain the month and the day. With $requireTime it will also ouput the time. This is useful for some views, e.g. scheduled downtimes. --- library/Icinga/Date/DateFormatter.php | 12 +++++++----- library/Icinga/Web/View/helpers/format.php | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/library/Icinga/Date/DateFormatter.php b/library/Icinga/Date/DateFormatter.php index 3c5287e86..a3a0a7cd4 100644 --- a/library/Icinga/Date/DateFormatter.php +++ b/library/Icinga/Date/DateFormatter.php @@ -40,10 +40,11 @@ class DateFormatter * Get the diff between the given time and the current time * * @param int|float $time + * @param bool $requireTime * * @return array */ - protected static function diff($time) + protected static function diff($time, $requireTime = false) { $invert = false; $now = time(); @@ -56,9 +57,9 @@ class DateFormatter if ($diff > 3600 * 24 * 3) { $type = static::DATE; if (date('Y') === date('Y', $time)) { - $formatted = date('M j', $time); + $formatted = date($requireTime ? 'M j H:i' : 'M j', $time); } else { - $formatted = date('Y-m', $time); + $formatted = date($requireTime ? 'Y-m-d H:i' : 'Y-m', $time); } } else { $minutes = floor($diff / 60); @@ -219,12 +220,13 @@ class DateFormatter * * @param int|float $time * @param bool $timeOnly + * @param bool $requireTime * * @return string */ - public static function timeUntil($time, $timeOnly = false) + public static function timeUntil($time, $timeOnly = false, $requireTime = false) { - list($type, $until, $invert) = static::diff($time); + list($type, $until, $invert) = static::diff($time, $requireTime); if ($invert && $type === static::RELATIVE) { $until = '-' . $until; } diff --git a/library/Icinga/Web/View/helpers/format.php b/library/Icinga/Web/View/helpers/format.php index 91645ad44..483b11338 100644 --- a/library/Icinga/Web/View/helpers/format.php +++ b/library/Icinga/Web/View/helpers/format.php @@ -60,13 +60,13 @@ $this->addHelperFunction('timeSince', function ($time, $timeOnly = false) { ); }); -$this->addHelperFunction('timeUntil', function ($time, $timeOnly = false) { +$this->addHelperFunction('timeUntil', function ($time, $timeOnly = false, $requireTime = false) { if (! $time) { return ''; } return sprintf( '%s', DateFormatter::formatDateTime($time), - DateFormatter::timeUntil($time, $timeOnly) + DateFormatter::timeUntil($time, $timeOnly, $requireTime) ); });