Make timeUntil behave like timeSince

This commit is contained in:
Thomas Gelf 2014-03-09 21:12:24 +01:00
parent df0dbaad5c
commit 7449f2565a

View File

@ -96,32 +96,36 @@ class Format
return floor($hour / 24) . 'd ' . ($hour % 24) . 'h'; return floor($hour / 24) . 'd ' . ($hour % 24) . 'h';
} }
public static function timeSince($timestamp) protected static function smartTimeDiff($diff, $timestamp)
{ {
if ($timestamp === null || $timestamp === false) { if ($timestamp === null || $timestamp === false) {
return '-'; return '-';
} }
if (! preg_match('~^\d+$~', $timestamp)) { if (! preg_match('~^\d+$~', $timestamp)) {
throw new ProgrammingError(sprintf('"%s" is not a number', $timestamp)); throw new ProgrammingError(sprintf('"%s" is not a number', $timestamp));
} }
$duration = time() - $timestamp;
$prefix = ''; $prefix = '';
if ($duration < 0) { if ($diff < 0) {
$prefix = '-'; $prefix = '-';
$duration *= -1; $diff *= -1;
} }
if ($duration > 3600 * 24 * 3) { if ($diff > 3600 * 24 * 3) {
if (date('Y') === date('Y', $timestamp)) { if (date('Y') === date('Y', $timestamp)) {
return date('d.m.', $timestamp); return date('d.m.', $timestamp);
} }
return date('m.Y', $timestamp); return date('m.Y', $timestamp);
} }
return $prefix . self::showHourMin($duration); return $prefix . self::showHourMin($diff);
}
public static function timeSince($timestamp)
{
return self::smartTimeDiff(time() - $timestamp, $timestamp);
} }
public static function timeUntil($timestamp) public static function timeUntil($timestamp)
{ {
return self::duration($timestamp - time()); return self::smartTimeDiff($timestamp - time(), $timestamp);
} }
protected static function formatForUnits($value, & $units, $base) protected static function formatForUnits($value, & $units, $base)