Setlocale does not affect PHP

I have the following code snippet:

setlocale(LC_ALL, "de"); print(strftime("%A %e %B %Y", time())); 

and seal

Tuesday May 4, 2010

instead

Dienstag 4. Mai 2010

Any ideas why? How to fix?

+6
php locale
source share
8 answers

You have the de locale; what does setlocale return for you? See: return values ​​for setlocale () .

Also, check the list of available locales (e.g. locale -a or what suits your OS) to see if there are de among them. Possible alternatives include de_DE or de_DE.utf8 , to name a few.

On Debian, to create a new locale, run the following command:

 dpkg-reconfigure locales 

and select the ones you need.

+14
source share

Setting the locale will not have any effect if the locale is not installed on your system.

+5
source share

For me, the following trick:

 setlocale(LC_TIME, ""); 

In combination with:

 echo strftime("%d. %B %Y"); 

This is how I got the current date in German format. Hope this helps.

+4
source share

Try setting LC_ALL to "de_DE". On my system, this did not work until I did.

 $ LC_ALL=de date Tue May 4 07:40:13 CDT 2010 $ LC_ALL=de_DE date Di 4. Mai 07:39:27 CDT 2010 
+3
source share

I am using Ubuntu on a Raspberry Pi, had the same problem trying to use portuguese local for date:

 setlocale(LC_TIME, "C"); echo strftime("%A"); echo setlocale(LC_TIME, "pt_PT"); echo strftime(" in Portuguese %A"); 

Then we checked it with the local -a command, pt_PT was not in the list, so I added it sudo / usr / share / locales / install-language-pack pt_PT and run local -a again: there was pt_PT.utf8 . After that, the result is still the same: the output is expected for pt_PT in English. Here is the small difference that made me work:

 ··· echo setlocale(LC_TIME, "pt_PT.utf8"); ··· 

So, I had to turn pt_PT into pt_PT.utf8

+3
source share
 locale -a locale-gen nb_NO.UTF-8 locale-gen nb_NO update-locale locale -a restart php5-fpm 
+1
source share

maybe you don’t have the locale installed, so if you are on ubuntu you can check the list with "locale -a" without the coyotes and check the available languages ​​in the / usr / share / i 18n / SUPPORTED file and they generate the locale required using "locale-gen de_DE"

Hope this work is for you.

0
source share

Keep in mind that you may have to restart the httpd and php-fpm defans after creating new locales on Linux.

Without restarting php (7.2), they could not be found, even if they are specified in locale -a.

Hope this saves some time :)

0
source share

All Articles