Use IntlDateFormatter in Format::timeUntil()

refs 
This commit is contained in:
Eric Lippmann 2015-04-08 15:19:51 +02:00
parent 1835479738
commit 3f2d87777a

@ -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)