Skip to content

Instantly share code, notes, and snippets.

@allejo
Created September 5, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allejo/896b10f5630bbbf8fb632de5ff4c83fe to your computer and use it in GitHub Desktop.
Save allejo/896b10f5630bbbf8fb632de5ff4c83fe to your computer and use it in GitHub Desktop.
<?php
class DateParserFilter extends \Twig_Extension
{
public function getFilters ()
{
return array(
new \Twig_SimpleFilter('parse_date', array($this, 'parseDate'))
);
}
public function parseDate ($string, $formats)
{
if (is_string($formats))
{
$formats = array($formats);
}
foreach ($formats as $format)
{
$dateTime = \DateTime::createFromFormat($format, $string);
if ($dateTime !== false)
{
return $dateTime;
}
}
return $string;
}
public function getName ()
{
return "parse_date";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment