Outputting dates in non-ASCII characters with PHP

前提是你 提交于 2019-12-02 01:39:29

Locales not only come in different languages, but also different encodings. The default zh_TW locale will probably use some Chinese encoding, while what you want is UTF-8. Hence, use the UTF-8 version of the locale:

setlocale(LC_TIME, 'zh_TW.UTF-8');

This depends on your specific system and what locales are installed on it. Check that on the command line:

$ locale -a
...
zh_TW
zh_TW.Big5
zh_TW.UTF-8

To be more cross-platform compatible, you can try several locales:

setlocale(LC_TIME, 'zh_TW.UTF-8', 'zh_TW.utf8', 'zh_TW');

You must probably tell PHP that all strings are UTF-8. E.g.:

    mb_internal_encoding('UTF-8');

Or if the problem is just with this string:

    $out = strftime("%e %B %Y", $timestamp);
    echo mb_convert_encoding($out, 'UTF-8', mb_detect_encoding($out));

Or if mb_detect_encoding() does not work correctly:

    $out = strftime("%e %B %Y", $timestamp);
    echo mb_convert_encoding($out, 'UTF-8', 'CNS-11643');

Before version 7 PHP basically does not know the encoding of the strings. Everything is just an array of 8 bit bytes. Getting the right output encoding sometimes works OK at once, but at other times you have to set everything up by hand.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!