mirror of
https://github.com/YGGverse/HLState.git
synced 2026-04-01 01:25:28 +00:00
add ago time format
This commit is contained in:
parent
d83764e453
commit
6714546199
4 changed files with 146 additions and 5 deletions
115
src/Twig/AppExtension.php
Normal file
115
src/Twig/AppExtension.php
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
ContainerInterface $container,
|
||||
TranslatorInterface $translator
|
||||
)
|
||||
{
|
||||
$this->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)]];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue