Formatting a DateTime Object Given Locale :: getDefault ()

I have a DateTime object that I am currently generating with

$mytime->format("D dmY") 

Which gives me exactly the format I need:

Tuesday 5.3.2012

The only missing point is the correct language. I need a German translation of Tue ( Tuesday ), which is Die ( Dienstag ).

This gives me the correct locale setting

 Locale::getDefault() 

But I do not know how to tell DateTime::format use it.

Is there no way to do something like:

 $mytime->format("D dmY", \Locale::getDefault()); 
+44
php datetime-format
Jan 05 2018-12-12T00:
source share
2 answers

This is because format does not pay attention to the locale. You must use strftime .

For example:

 setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set $formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp()) 
+45
Jan 05 2018-12-15T00:
source share

You can use the Intl extension to format the date. It will format the dates / times according to the selected locale, or you can override this with IntlDateFormatter::setPattern() .

A quick example of using a custom template for the desired output format may seem like.

 $dt = new DateTime; $formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); $formatter->setPattern('E dMyyyy'); echo $formatter->format($dt); 

Which deduces the following (today, at least).

Di. 4/06/2013




Edit : Ahh boo! The ancient question answered, because some comments pushed him to the list! At least Intl option is now specified.

+79
Jun 04 '13 at 15:33
source share



All Articles