Home  >  Q&A  >  body text

Is the setlocale() function in PHP invalid for Chinese?

Environment:
CentOS 7.3.1611
PHP 7.1.3

Code:

echo setlocale(LC_ALL, array('zh_CN.UTF-8', 'zh_CN.utf8', 'zh_CN')), PHP_EOL;
$date = new DateTime();
echo $date->format('Y-F-d l'), PHP_EOL;

Output:

zh_CN.UTF-8
2017-May-22 Monday

Additional tests, in Shell:

# export LC_ALL=zh_CN.UTF_8
# date
2017年 05月 22日 星期一 19:02:15 CST
# export LC_ALL=en_US.UTF_8
# date
Mon May 22 19:02:34 CST 2017

Question:
It is possible to localize date output in Bash.
Why is it invalid to use the setlocale() function in PHP?

Please give me some advice.

怪我咯怪我咯2680 days ago861

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-05-24 11:35:31

    Neither of the above two answers hit the point.
    setlocale setlocale
    strftimestrftime - Format local time/date according to locale

    These two functions must be used together to see the effect.

    However, even so, the performance is still different from when you directly call the system bash, and may not necessarily meet your expectations.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-24 11:35:31

    The date command in bash calls a date application in linux to realize the display and output of the date function

    php is the date object and function implemented by itself

    It’s quite understandable that the functions of the two are different!

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-24 11:35:31

    These are two languages ​​with different method mechanisms. If you are using php里面想得到中文这种格式,完全可以在php中通过exec调用shell’s method:

    $cmd = "export LC_ALL=zh_CN.UTF_8 && date";
    $date = exec($cmd);

    reply
    0
  • Cancelreply