7

Can someone tell me how to switch the locale date format in Symfony2?

no matter what I set in config (framework.session.default_locale), this always returns week days in english ( I would need the weekday in german language):

$start->format("D d.m.Y");

when I output the default locale it always returnes "de" BTW, no matter what I setup in the config.yml

echo  \Locale::getDefault()

ADDITIONAL INFOS:

I want to be able to format the date in a way that it gets outputted correctly (meaning with correct german weekdays) on my local system AND on the live server. Tried this with strftime which uses the system's installed locales and had real troubles with it, since the locale packages have different naming schemes on the live and dev machines. There is another SO question regarding this here: PHP: why is my date formated on the server differently?

Community
  • 1
  • 1
stoefln
  • 14,498
  • 18
  • 79
  • 138

5 Answers5

8

Symfony2 does not provide any date formatter in its core.

All it does is to configure the \Locale::getDefault() value by sniffing session and request.

You have to provide some extra services to handle per locale formatting, like the SonataIntlBundle .

This bundle adds some twig filters in an extension, and provide some helpers that can be used anywhere.

Once activated, you can use them in your templates for example:

{{ my_date|format_datetime }}

More infos here: https://github.com/sonata-project/SonataIntlBundle/blob/master/Resources/doc/reference/datetime.rst

Florian Klein
  • 8,692
  • 1
  • 32
  • 42
  • 2
    Thanks for pointing out that there is no Symfony-out-of-the-box functionality for this. But there has to be some way to get this running using Intl. I looked at the code of SonataIntlBundle, but I don't get it. I'm feeling like on mars- am I really the only person who is having this issue?? – stoefln Feb 16 '12 at 12:56
  • A lot of things from sonata can make you think you are on mars :) But many parts of symfony related code is like that :) Of course, you could simply use Intl* classes directly, and configure them using $session->getLocale() or whatever. – Florian Klein Feb 16 '12 at 14:22
  • Now I got it, there was some piece of valueable information missing for me: the users locale gets set on login, so changing the configuration does not have any effect till the user logs out and in again. Thanks! – stoefln Feb 23 '12 at 14:09
4

I used such code to format dates in my project (You will need to turn on intl extension for php):

<?php
    class DefaultController extends Controller {
        public function indexAction() {
            $date = new \DateTime("now");
            $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::NONE, \IntlDateFormatter::NONE);
            $formatter->setPattern("EEEE Y-M-d");
            return array('locale' => \Locale::getDefault(), "intl" => $formatter->format($date));
    }

And it works fine for me. How to create formatting patter you can find at http://userguide.icu-project.org/formatparse/datetime

But I didn't found a helper for twig, so you can create your own

a.tereschenkov
  • 807
  • 4
  • 10
2

A more simple way is to store the formats in the translation files, like this:

In the "messages.en.yml" file:

date.format: m/d/Y
time.format: g:i A

In the "messages.fr.yml" file:

date.format: d/m/Y
time.format: H:i

Then, in a twig file, you can do this:

{{ my_date|date('date.format'|trans) }}

So the date will automatically be in the right format, using the current locale.

Gregoire
  • 3,735
  • 3
  • 25
  • 37
  • This is the most easiest way IMO. And it gives you the possibility to define formats like long, short etc. and just use them without any additional bundles. – Stefan Staub Apr 01 '15 at 11:08
  • 1
    The only problem here is that the names of days and months don't get translated. – Stefan Staub Apr 01 '15 at 11:15
2

simply Add twig extension in your config.yml

services:
    twig.extension.intl:
        class: Twig_Extensions_Extension_Intl
        tags:
            - { name: twig.extension }

In your template twig Syntaxe : {{ myDate | localizeddate(‘sizeDate‘, ‘sizeHour‘, ‘locale‘ }} myDate : must be a DateTime object sizeDate : can be none/short/medium/long/full sizeHour : can be none/short/medium/long/full locale : optional by default the session language will be use but you can force one diffrent locale value

{{ myDate | localizeddate('full', 'none') }} ({{ myDate | localizeddate('none', 'short') }})
<!--
locale "fr" : vendredi 21 décembre 2012 (13:37)
locale "en : Friday, December 21, 2012 (13:37 PM)
-->

If you get an error "Please install the ‘intl’ extension for full localization capabilities"

install php5-intl package eg. on Debian server the command will be : apt-get install php5-intl

Dont forget to clear your cache ;)

Fabrice G
  • 273
  • 2
  • 5
0

There is a bundle for that purpose: https://github.com/michelsalib/BCCExtraToolsBundle Easier to use than sonata project. :)

ihsan
  • 2,279
  • 20
  • 36