diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php index b999626b9..43b579208 100644 --- a/library/Icinga/Util/Format.php +++ b/library/Icinga/Util/Format.php @@ -4,6 +4,7 @@ namespace Icinga\Util; use DateTime; +use IntlDateFormatter; use Icinga\Exception\ProgrammingError; class Format @@ -126,9 +127,35 @@ class Format return $result; } - public static function timeUntil($timestamp) + public static function timeUntil($time, $now = null) { - return self::smartTimeDiff($timestamp - time(), $timestamp); + $time = (float) $time; + if ($now === null) { + $now = time(); + } + $diff = $time - $now; + if ($diff < 0) { + $diff = abs($diff); + // return static::timeAgo($time, $now); + } + if ($diff > 3600 * 24 * 3) { + $fmt = new IntlDateFormatter('en_US', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); + $until = $fmt->format($time); + } else { + $minutes = floor($diff / 60); + if ($minutes < 60) { + $until = sprintf('%dm %ds', $minutes, $diff % 60); + } else { + $hours = floor($minutes / 60); + if ($hours < 24) { + $fmt = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::SHORT); + $until = $fmt->format($time); + } else { + $until = sprintf('%dd %dh', floor($hours / 24), $hours % 24); + } + } + } + return $until; } public static function prefixedTimeUntil($timestamp, $ucfirst)