Created
September 5, 2016 20:42
-
-
Save allejo/896b10f5630bbbf8fb632de5ff4c83fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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