How to force PHP to use internationalized dates?

I am trying to get PHP dates to work in different languages. The language code will be provided in accordance with the language set by the user.

I thought I could do this:

setlocale(LC_ALL, 'de_DE.UTF-8'); echo strftime('%A %B %Y'); 

But the way out:

 Wednesday April 2011 

While I was expecting:

 Mittwoch April 2011 

(April is the same in English and German)

Is this the wrong way to use the strftime function? If not, is there an alternative method?

+4
source share
4 answers

setlocale () returns a value that can be FALSE:

Returns the new current language or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist, or an invalid category name.

So you need to check the return value.

Remember that locale names vary by platform, and de_DE.UTF-8 looks like a typical Unix name. Is this a Unix server? If so, make sure that the language standard is set on the computer itself.

+2
source

You can use the IntlDateFormatter class (PHP> = 5.3)


To quote an example from the IntlDateFormatter::format() page:

 $fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL,IntlDateFormatter::FULL,'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); echo "First Formatted output is ".$fmt->format(0); $fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL,IntlDateFormatter::FULL,'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); echo "Second Formatted output is ".$fmt->format(0); 

It will display:

 First Formatted output is Wednesday, December 31, 1969 4:00:00 PM PT Second Formatted output is Mittwoch, 31. Dezember 1969 16:00 Uhr GMT-08:00 
+4
source

It looks like you are missing the code. The answer can be found here:
http://php.net/manual/en/function.setlocale.php

/ * try different possible locale names for german as of PHP 4.3.0 * / $ loc_de = setlocale (LC_ALL, ' de_DE@euro ', 'de_DE', 'de', 'ge'); echo "Preferred locale for german on this system is '$ loc_de'";

? >

0
source

Your use of strftime () seems correct according to the manual. I would ask a question about setLocale () settings.

See here for a question very similar to yours on another forum.

0
source

Source: https://habr.com/ru/post/1347945/


All Articles