Date switching

Can someone tell me how to change date format in symfony2?

no matter what I set in config (framework.session.default_locale), it always returns week days in English (I need a weekday in German):

$start->format("D dmY"); 

when I output the standard locale, it always returns "de" BTW, regardless of what I configure in config.yml

 echo \Locale::getDefault() 

ADDITIONAL INFORMATION:

I want to be able to format the date so that it is displayed correctly (which means the correct weekend in Germany) in my local system AND on a real server. I tried this with strftime, which uses locales installed by the system and had real problems with it, because locale packages have different naming schemes on live and dev machines. There is another question here: PHP: why is my date configured on the server differently?

+7
source share
5 answers

I used this code to format dates in my project (you need to enable the intp 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 YMd"); return array('locale' => \Locale::getDefault(), "intl" => $formatter->format($date)); } 

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

But I did not find a helper for the branch, so you can create your own

+3
source

Symfony2 does not provide date formats in its core.

All he does is set the value \Locale::getDefault() using sniffing session and request.

You need to provide some additional services for handling formatting by language, such as SonataIntlBundle .

This kit adds some twist filters to the extension and provides some helpers that you can use anywhere.

After activation, you can use them in your templates, for example:

 {{ my_date|format_datetime }} 

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

+8
source

just add twig extension to config.yml

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

In your template branch Syntax: {{myDate | localizeddate ('sizeDate', 'sizeHour', 'locale'}} myDate: must be a DateTime object sizeDate: may be none / short / medium / long / full sizeHour: may be none / short / medium / long / full locale: optional By default, the session language will be used, but you can force one value of the locale

 {{ 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 receive the error message ' Please install the extension' intl for full localization capabilities

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

Do not forget to clear the cache;)

+2
source

An easier way is to save the formats in the translation files, for example:

In the ru .yml message file:

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

In the fr .yml message file:

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

Then in the twig file you can do this:

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

Thus, the date will automatically be in the correct format using the current locale.

+1
source

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

0
source

All Articles