Twig datetime过滤器可以隐藏午夜时间

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?

you could do something where the date is passed in separately to the time,

datetime("m/d/Y", "g:i A")

and then mod the current timestamp by 86400 (one day in seconds) and if it's 0, then you know it's midnight and you can display the "time" param instead of just the date param.

pseudocode:

datetime(ts, day, time):
    if (ts % 86400 == 0):
        return format_ts(ts, day)
    else:
        return format_ts(ts, day + " " + time)

As I was the one to ask this question, I will not be selecting the following unless there are no better answers in the next 40 days.

$twig->addFilter(new Twig_SimpleFilter('datetime', function ($d,$f='m/d/Y g:i:s A') {
    $f=(strlen($d)==19 && substr($d, -8)=='00:00:00')?substr($f, 0, strpos($f, ' ')):$f;
    $date = new DateTime($d);
    return $date->format($f);
}));