这段代码在我的开发服务器上运行得很好,但是当我在另一台服务器上运行它时,它失败了:
$formatter = new IntlDateFormatter('en-GB', IntlDateFormatter::LONG, IntlDateFormatter::NONE, 'Europe/London');
var_dump($formatter->format(new DateTime('2012-06-01')));
var_dump($formatter->getErrorMessage() . ' ' . $formatter->getErrorCode());
给我:
bool(false)
string(14) "U_ZERO_ERROR 0"
有谁知道为什么?特别奇怪的是它甚至不报告错误(U_ZERO_ERROR 是默认值)。谢谢。
编辑:
为了比较,我在本地得到:
string(11) "1 June 2012"
string(14) "U_ZERO_ERROR 0"
我在其他语言环境中也看到了同样的问题,包括
en-US
。 最佳答案
好的,想通了。只有 PHP 5.3.4+ 允许将 DateTime 对象传递给格式。在旧版本上,您需要执行以下操作:
$date = new DateTime('2012-06-01');
$formatter->format($date->getTimestamp());
关于PHP IntlDateFormatter->format() 返回 false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12600859/