PHP locale de_DE is returning English locale

2019-03-02 12:40发布

问题:

I am using Apache and php 7 on an Ubuntu System. After installing the german locales and running locale -a to check the installed locales, I get C, C.UTF-8, de_DE, de_DE@euro, de_DE.iso88591, de_DE.iso885915@euro, de_DE.utf8, deutsch, en_US.utf8, german, POSIX. I also update the locale using sudo update-locale. However using the php code

setlocale(LC_ALL,'de_DE');
echo date("F", strtotime("2018-10-10"));

It returns me the English "October", not the German "Oktober". I have no idea what I can do... I also restarted the Apache... but nothing happened.

回答1:

The date() function doesn't listen to the locales - this will be returning values in English only.

From the date() documentation,

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

You need to use strftime() instead.

setlocale(LC_ALL,'de_DE');
echo strftime("%B", strtotime("2018-10-10"));
  • strftime() documentation