使用PHP以非ASCII字符输出日期 [英] Outputting dates in non-ASCII characters with PHP

查看:337
本文介绍了使用PHP以非ASCII字符输出日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用繁体中文输出日期.

I'm trying to output the date in Traditional Chinese.

我将日期作为Unix时间戳记(例如:"1467244800").

I have the date as a Unix timestamp, ( example: "1467244800" ).

我正在执行以下操作:

<?php
setlocale (LC_TIME, "zh_TW");
echo strftime("%e %B %Y", $timestamp );
?>

我要输出的是Unicode未定义"字符:

What I'm getting output is the Unicode "Undefined" characters:

30 ���� 2016
17 �T�� 2016
18 �Q�G�� 2015 

有人可以告诉我我在做什么错吗?

Can anyone tell me what I'm doing wrong?

我的HTML标头包含:

My HTML headers contain:

<html lang="zh-TW">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

该页面上其余的中文内容输出正常.如果您查看页面源代码,则会看到:

And the rest of my Chinese content on the page is outputting fine. If you view the page source you see:

<span>最新消息</span>
<span class="pipe">18 �Q�G�� 2015</span>

只有strftime()日期显示为未定义"字符: ...

It's only the strftime() dates that appear as "Undefined" characters: ����...

感谢您的关注...

推荐答案

语言环境不仅使用不同的语言,而且使用不同的编码.默认的zh_TW语言环境可能会使用某些中文编码,而您想要的是UTF-8.因此,请使用语言环境的UTF-8版本:

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');

这篇关于使用PHP以非ASCII字符输出日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆