diff --git a/config/services.yaml b/config/services.yaml index 31fbbda..1897bea 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -30,3 +30,9 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones + + App\Twig\AppExtension: + arguments: + - '@service_container' + tags: + - { name: twig.extension} \ No newline at end of file diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php new file mode 100644 index 0000000..1a62a16 --- /dev/null +++ b/src/Twig/AppExtension.php @@ -0,0 +1,115 @@ +container = $container; + $this->translator = $translator; + } + + public function getFilters() + { + return + [ + new TwigFilter( + 'format_ago', + [ + $this, + 'formatAgo' + ] + ) + ]; + } + + public function formatAgo( + int $time, + ): string + { + $diff = time() - $time; + + if ($diff < 1) + { + return $this->translator->trans('now'); + } + + $values = + [ + 365 * 24 * 60 * 60 => + [ + $this->translator->trans('year ago'), + $this->translator->trans('years ago'), + $this->translator->trans(' years ago') + ], + 30 * 24 * 60 * 60 => + [ + $this->translator->trans('month ago'), + $this->translator->trans('months ago'), + $this->translator->trans(' months ago') + ], + 24 * 60 * 60 => + [ + $this->translator->trans('day ago'), + $this->translator->trans('days ago'), + $this->translator->trans(' days ago') + ], + 60 * 60 => + [ + $this->translator->trans('hour ago'), + $this->translator->trans('hours ago'), + $this->translator->trans(' hours ago') + ], + 60 => + [ + $this->translator->trans('minute ago'), + $this->translator->trans('minutes ago'), + $this->translator->trans(' minutes ago') + ], + 1 => + [ + $this->translator->trans('second ago'), + $this->translator->trans('seconds ago'), + $this->translator->trans(' seconds ago') + ] + ]; + + foreach ($values as $key => $value) + { + $result = $diff / $key; + + if ($result >= 1) + { + $round = round($result); + + return sprintf( + '%s %s', + $round, + $this->plural( + $round, + $value + ) + ); + } + } + } + + private function plural(int $number, array $texts) + { + $cases = [2, 0, 1, 1, 1, 2]; + + return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; + } +} \ No newline at end of file diff --git a/templates/default/main/index.html.twig b/templates/default/main/index.html.twig index 9a16a83..fdc4ed5 100644 --- a/templates/default/main/index.html.twig +++ b/templates/default/main/index.html.twig @@ -41,8 +41,16 @@ {% endif %} {{ server.info.Map }} - {{ server.added | format_date }} - {{ server.online | format_date }} + + + {{ server.added | format_ago }} + + + + + {{ server.online | format_ago }} + + {{ server.connections }}{#{{ server.info.Players }}#} {{ server.info.MaxPlayers }} {{ server.info.Bots }} diff --git a/templates/default/server/index.html.twig b/templates/default/server/index.html.twig index f09213e..258ca99 100644 --- a/templates/default/server/index.html.twig +++ b/templates/default/server/index.html.twig @@ -135,7 +135,11 @@ {% for online in server.online %} - {{ online.time | date('r') }} + + + {{ online.time | format_ago }} + + {{ online.players }} {{ online.bots }} {{ online.total }} @@ -170,8 +174,16 @@ {{ player.frags }} {{ player.name }} - {{ player.joined | format_date }} - {{ player.online | format_date }} + + + {{ player.joined | format_ago }} + + + + + {{ player.online | format_ago }} + + {% endfor %}