Is there a Twig alternative for DateTime :: createFromFormat (...)?

I have a line indicating the week number and year, and you want to print Sunday this week.

An example in PHP:

$dateString = '502016'; $dateObject = DateTime::createFromFormat('WY', $dateString); echo $dateObject->format('Ym-d'); 

Sort of:

 {{ published_at|format(date("Ymd"), 'dm-Y') }} 

Is this possible in twig?

+5
source share
1 answer

I found something that might help you: override the inline branch date filter

Creates a new branch extension for what you want :)

PS: At the end, it returns a PHP DateTime

 <?php /* * Extension to provide updated date & date_modify filters along with an * updated date function which do not auto-convert strings of numbers to * a unix timestamp. * * Code within dateFilter(), modifyFilter() and dateFromString() extracted * from Twig/lib/Twig/Extension/Core.php which is (c) 2009 Fabien Potencier * and licensed as per https://github.com/twigphp/Twig/blob/master/LICENSE */ namespace My\Twig\Extension; use Twig_Extension; use Twig_SimpleFilter; use Twig_SimpleFunction; use DateTime; use DateTimeInterface; use DateTimeImmutable; class DateExtension extends Twig_Extension { public function getName() { return 'my_date'; } public function getFilters() { return array( new Twig_SimpleFilter('date', [$this, 'dateFilter'], ['needs_environment' => true]), new Twig_SimpleFilter('date_modify', [$this, 'modifyFilter'], ['needs_environment' => true]), ); } public function getFunctions() { return array( new Twig_SimpleFunction('date', [$this, 'dateFromString'], ['needs_environment' => true]), ); } public function dateFilter($env, $date, $format = null, $timezone = null) { if (null === $format) { $formats = $env->getExtension('core')->getDateFormat(); $format = $date instanceof DateInterval ? $formats[1] : $formats[0]; } if ($date instanceof DateInterval) { return $date->format($format); } return $this->dateFromString($env, $date, $timezone)->format($format); } public function modifyFilter($env, $date, $format = null, $timezone = null) { $date = $this->dateFromString($env, $date, false); $date->modify($modifier); return $date; } public function dateFromString($env, $date, $timezone) { // determine the timezone if (!$timezone) { $defaultTimezone = $env->getExtension('core')->getTimezone(); } elseif (!$timezone instanceof DateTimeZone) { $defaultTimezone = new DateTimeZone($timezone); } else { $defaultTimezone = $timezone; } // immutable dates if ($date instanceof DateTimeImmutable) { return false !== $timezone ? $date->setTimezone($defaultTimezone) : $date; } if ($date instanceof DateTime || $date instanceof DateTimeInterface) { $date = clone $date; if (false !== $timezone) { $date->setTimezone($defaultTimezone); } return $date; } $date = new DateTime($date, $defaultTimezone); if (false !== $timezone) { $date->setTimezone($defaultTimezone); } return $date; } } 

This class simply registers a new date, date_modify filters and a new one to replace them in the Twig kernel, and then is a direct copy of the twig_date_format_filter, twig_date_modify_filter and twig_date_converter functions with remote functionality.

I also needed to register this extension with Twig_Environment with:

$env->addExtension(new \My\Twig\Extension\DateExtension());

and i finished.

 {{ "20141216"|date('jS F Y') }} {{ "20141216"|date('jS F Y') }} 

now correctly issues: December 16, 2014 December 16, 2014

So far, ashamed, I cannot just override twig_date_converter , I am glad that I can reregister relevant Twig filters and functions.

+2
source

Source: https://habr.com/ru/post/1259155/


All Articles