Skip to content

Commit

Permalink
Create DateExtension.php
Browse files Browse the repository at this point in the history
  • Loading branch information
raizdev authored Oct 31, 2022
1 parent 0eb47bb commit dacfb02
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Extensions/DateExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
namespace Raizdev\Twig\Extensions;

use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* @author Robin van der Vleuten <[email protected]>
*/
class DateExtension extends AbstractExtension
{
public static $units = [
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
];

private $translator;

public function __construct(TranslatorInterface $translator = null)
{
// Ignore the IdentityTranslator, otherwise the parameters won't be replaced properly
if ($translator instanceof IdentityTranslator) {
$translator = null;
}

$this->translator = $translator;
}

public function getFilters()
{
return [
new TwigFilter('time_diff', [$this, 'diff'], ['needs_environment' => true]),
];
}

/**
* Filters for converting dates to a time ago string like Facebook and Twitter has.
*
* @param string|DateTime $date a string or DateTime object to convert
* @param string|DateTime $now A string or DateTime object to compare with. If none given, the current time will be used.
*
* @return string the converted time
*/
public function diff(Environment $env, $date, $now = null)
{
// Convert both dates to DateTime instances.
$date = twig_date_converter($env, $date);
$now = twig_date_converter($env, $now);

// Get the difference between the two DateTime objects.
$diff = $date->diff($now);

// Check for each interval if it appears in the $diff object.
foreach (self::$units as $attribute => $unit) {
$count = $diff->$attribute;

if (0 !== $count) {
return $this->getPluralizedInterval($count, $diff->invert, $unit);
}
}

return '';
}

private function getPluralizedInterval($count, $invert, $unit)
{
if ($this->translator) {
$id = sprintf('diff.%s.%s', $invert ? 'in' : 'ago', $unit);

return $this->translator->transChoice($id, $count, ['%count%' => $count], 'date');
}

if (1 !== $count) {
$unit .= 's';
}

return $invert ? "in $count $unit" : "$count $unit ago";
}
}

0 comments on commit dacfb02

Please sign in to comment.