Question

I'm trying to show a localized date with strftime but it does not work.

class ExampleController extends AbstractActionController
{
    public function indexAction()
    {
        $openDate = DateTime::createFromFormat(...);
        setlocale(LC_ALL, Locale::getDefault());
        Debug::dump(Locale::getDefault()); // shows 'fr_FR'
        Debug::dump(strftime('%B %Y', $openDate->getTimestamp())); // shows 'August 2013' instead of 'Août 2013'

    }
}

In module/Application/config/module.config.php

return array(

    ...

    'translator' => array(
        'locale' => 'fr_FR',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

    ...

);

Is someone can tell me why the month is not translated in French ?

Was it helpful?

Solution

strftime has nothing to do with ZF2 or the intl php extension. However ZF2 does come with a DateFormat view helper that should solve your problem. The full documentation is available at:

http://framework.zend.com/manual/2.2/en/modules/zend.i18n.view.helpers.html#dateformat-helper

A quick example:

Debug::dump($this->dateFormat($openDate->getTimestamp(), IntlDateFormatter::LONG));

The default locale for the DateFormat view helper is that returned by Locale::getDefault() therefore it should return the date in french as you require.

To use your custom format:

Debug::dump($this->dateFormat($openDate->getTimestamp(), IntlDateFormatter::LONG, IntlDateFormatter::LONG, null, "dd LLLL Y - HH:mm"));

OTHER TIPS

According what Tomdarkness said, I found a way to do what I wanted. I used IntlDateFormatter :

$formatter = \IntlDateFormatter::create(
    \Locale::getDefault(), // fr_FR
    \IntlDateFormatter::FULL,
    \IntlDateFormatter::FULL,
    'Europe/Paris',
    \IntlDateFormatter::GREGORIAN,
    'dd LLLL Y - HH:mm'
);

echo $formatter->format($openDate); // shows '20 août 2013 - 14:39'

I think maybe I will code my own dateFormat view helper based on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top