From 5ec4648589e526dafde93ef04fa91043f6df9dde Mon Sep 17 00:00:00 2001 From: ghost Date: Thu, 30 Dec 2021 03:24:00 +0200 Subject: [PATCH] add time ago convertor --- src/system/helper/localization.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/system/helper/localization.php b/src/system/helper/localization.php index eff2664..f9ca812 100644 --- a/src/system/helper/localization.php +++ b/src/system/helper/localization.php @@ -8,4 +8,30 @@ class Localization { return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; } + + public static function time(int $time) { + + $timeDiff = time() - $time; + + if ($timeDiff < 1) { + return _('0 seconds'); + } + + $a = [365 * 24 * 60 * 60 => [_('year'), _('years'), _('years')], + 30 * 24 * 60 * 60 => [_('month'), _('months'), _('months')], + 24 * 60 * 60 => [_('day'), _('days'), _('days')], + 60 * 60 => [_('hour'), _('hours'), _('hours')], + 60 => [_('minute'), _('minutes'), _('minutes')], + 1 => [_('second'), _('seconds'), _('seconds')]]; + + foreach ($a as $secs => $v) { + + $d = $timeDiff / $secs; + + if ($d >= 1) { + $r = round($d); + return sprintf('%s %s ago', $r, self::plural($r, $v)); + } + } +} }