From 6a6e14cf0273d9f41da9fbcf93a8e864895cb4c3 Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Mon, 16 Nov 2015 17:43:25 +0100 Subject: [PATCH] Refactor time_elapsed_string -> timeElapsedString and with short version. --- core/ManiaExchange/ManiaExchangeList.php | 2 +- core/Utils/Formatter.php | 34 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/ManiaExchange/ManiaExchangeList.php b/core/ManiaExchange/ManiaExchangeList.php index 9d061dd4..736f06e3 100644 --- a/core/ManiaExchange/ManiaExchangeList.php +++ b/core/ManiaExchange/ManiaExchangeList.php @@ -204,7 +204,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener $lineQuad->setZ(0.001); } - $time = Formatter::time_elapsed_string(strtotime($map->updated)); + $time = Formatter::timeElapsedString(strtotime($map->updated)); $array = array('$s' . $map->id => $posX + 3.5, '$s' . $map->name => $posX + 12.5, '$s' . $map->author => $posX + 59, '$s' . str_replace('Arena', '', $map->maptype) => $posX + 103, '$s' . $map->mood => $posX + 118, '$s' . $time => $posX + 130); $labels = $this->maniaControl->getManialinkManager()->labelLine($mapFrame, $array); $authorLabel = $labels[2]; diff --git a/core/Utils/Formatter.php b/core/Utils/Formatter.php index 7c307fc0..862bb14b 100644 --- a/core/Utils/Formatter.php +++ b/core/Utils/Formatter.php @@ -48,24 +48,36 @@ abstract class Formatter { /** * Format an elapsed time String (2 days ago...) by a given timestamp * - * @param int $ptime - * @return string + * @param int $time Input time + * @param boolean $short Short version + * @return string Formatted elapsed time string */ - public static function time_elapsed_string($ptime) { - // TODO: refactor code: camelCase! - $etime = time() - $ptime; + public static function timeElapsedString($time, $short = false) { + $elapsedTime = time() - $time; - if ($etime < 1) { - return '0 seconds'; + $second = $short ? 'sec.' : 'second'; + $minute = $short ? 'min.' : 'minute'; + $hour = $short ? 'h' : 'hour'; + $day = $short ? 'd' : 'day'; + $month = $short ? 'm' : 'month'; + $year = $short ? 'y' : 'year'; + + if ($elapsedTime < 1) { + return $short ? '0 sec.' : '0 seconds'; } - $a = array(12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second'); + $calculateSeconds = array(12 * 30 * 24 * 60 * 60 => $year, + 30 * 24 * 60 * 60 => $month, + 24 * 60 * 60 => $day, + 60 * 60 => $hour, + 60 => $minute, + 1 => $second); - foreach ($a as $secs => $str) { - $d = $etime / $secs; + foreach ($calculateSeconds as $secs => $str) { + $d = $elapsedTime / $secs; if ($d >= 1) { $r = round($d); - return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago'; + return $r . ' ' . $str . ($r > 1 ? (!$short ? 's' : '') : '') . ' ago'; } } return '';