Given some date value, I would like to display the date with some given format using Twig. http://twig.sensiolabs.org/doc/filters/date.html provides most of this functionality.
Now, I would like to go one step forward, and if somedate
is just equal to something like 2015-01-21 00:00:00
(i.e. midnight), not to display the time but just the date.
I came up with a partial solution where {{ somedate|datetime(true) }}
is used to display the datetime potentially with seconds, however, my solution is weak as the format cannot be modified.
$twig->addFilter(new Twig_SimpleFilter('datetime', function ($d,$s=null) {
$f='m/d/Y'.((strlen($d)==19 && substr($d, -8)!='00:00:00')?' g:i'.(($s)?':s':null).' A':null);
$date = new DateTime($d);
return $date->format($f);
}));
I would like to modify the filter so that a format is provided such as {{ data.bid_date|datetime("m/d/Y g:i A") }}
, and the datetime is displayed using the given format unless it is midnight in which only the date is showing.
How is this best accomplished?