CRUDlex\TwigExtensions::formatTime PHP Method

formatTime() protected method

If the value is false (like null), an empty string is returned. Else, the value is tried to be parsed as datetime via the given pattern. If that fails, it is tried to be parsed with the pattern 'Y-m-d H:i:s'. If that fails, the value is returned unchanged. Else, it is returned formatted with the given pattern. The effect is to shorten 'Y-m-d H:i:s' to 'Y-m-d' for example.
protected formatTime ( string $value, string $timezone, string $pattern ) : string
$value string the value to be formatted
$timezone string the timezone of the value
$pattern string the pattern with which the value is parsed and formatted
return string the formatted value
    protected function formatTime($value, $timezone, $pattern)
    {
        if (!$value) {
            return '';
        }
        $result = \DateTime::createFromFormat($pattern, $value, new \DateTimeZone($timezone));
        if ($result === false) {
            $result = \DateTime::createFromFormat('Y-m-d H:i:s', $value, new \DateTimeZone($timezone));
        }
        if ($result === false) {
            return $value;
        }
        $result->setTimezone(new \DateTimeZone(date_default_timezone_get()));
        return $result->format($pattern);
    }