Bolt\Twig\Handler\TextHandler::localeDateTime PHP Method

localeDateTime() public method

Returns the date time in a particular format. Takes the locale into account.
public localeDateTime ( string | DateTime $dateTime, string $format = '%B %e, %Y %H:%M' ) : string
$dateTime string | DateTime
$format string
return string Formatted date and time
    public function localeDateTime($dateTime, $format = '%B %e, %Y %H:%M')
    {
        if (!$dateTime instanceof \DateTime) {
            $dateTime = new \DateTime($dateTime);
        }
        // Check for Windows to find and replace the %e modifier correctly
        // @see: http://php.net/strftime
        $os = strtoupper(substr(PHP_OS, 0, 3));
        $format = $os !== 'WIN' ? $format : preg_replace('#(?<!%)((?:%%)*)%e#', '\\1%#d', $format);
        // According to http://php.net/manual/en/function.setlocale.php manual
        // if the second parameter is "0", the locale setting is not affected,
        // only the current setting is returned.
        $result = setlocale(LC_ALL, 0);
        if ($result === false) {
            // This shouldn't occur, but.. Dude!
            // You ain't even got locale or English on your platform??
            // Various things we could do. We could fail miserably, but a more
            // graceful approach is to use the datetime to display a default
            // format
            $this->app['logger.system']->error('No valid locale detected. Fallback on DateTime active.', ['event' => 'system']);
            return $dateTime->format('Y-m-d H:i:s');
        } else {
            $timestamp = $dateTime->getTimestamp();
            return strftime($format, $timestamp);
        }
    }

Usage Example

Beispiel #1
0
 public function testLocaleDateTimeObjectWithFormat()
 {
     $app = $this->getApp();
     $handler = new TextHandler($app);
     $dateTime = new \DateTime('2012-06-14 09:07:55');
     $result = $handler->localeDateTime($dateTime, '%Y-%m-%d %H:%M');
     $this->assertSame('2012-06-14 09:07', $result);
 }