php - PHP IntlDateFormatter 在不同的服务器上返回不同的值
问题描述
IntlDateFormatter
在这些不同的服务器上返回不同的 UTC 时间值的原因是什么(请参阅下面示例中的最后一个输出行)?
服务器 A
> php --version
PHP 7.3.6 (cli) (built: Jun 17 2019 21:27:20) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies
> php -a
Interactive shell
php > $fmt = new \IntlDateFormatter('en_US', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, 'UTC', \IntlDateFormatter::GREGORIAN);
php > echo $fmt->format(new \DateTime('July 1, 2019 12:00pm'));
Monday, July 1, 2019 at 12:00:00 PM GMT
服务器 B
> php --version
PHP 7.3.7 (cli) (built: Jul 3 2019 22:04:27) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.7, Copyright (c) 1999-2018, by Zend Technologies
> php -a
Interactive shell
php > $fmt = new \IntlDateFormatter('en_US', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, 'UTC', \IntlDateFormatter::GREGORIAN);
php > echo $fmt->format(new \DateTime('July 1, 2019 12:00pm'));
Monday, July 1, 2019 at 12:00:00 PM Coordinated Universal Time
解决方案
服务器对 DateTime 的默认时区有不同的默认设置。您必须为 DateTime 设置相同的时区才能获得相同的输出。仅为 IntlDateFormatter 设置时区是不够的。
<?php
$fmt = new \IntlDateFormatter(
'en_US',
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
'UTC',
\IntlDateFormatter::GREGORIAN
);
echo $fmt->format(new \DateTime('July 1, 2019 12:00pm', new \DateTimeZone('UTC')));
推荐阅读
- r - 在 Factoextra 上更改形状
- javascript - DOM 外的过渡
- python - 带有高速公路 python 的 websocket 中继
- css - 如何在 FontAwesome 中包含我的自定义字体
- python - 从 YouTube 视频中提取音频
- apache-spark - 如何在不使用 Azure Databricks 中的 Pyspark 缓存数据的情况下查询损坏的记录?
- airflow - 如何在 python 可调用文件中使用 postgres 连接 ID 连接到 postgres
- django-rest-auth - Django rest auth :: smtp.EmailBackend:如果在注册期间连接到 smtp 失败而不是在调试模式下至少 500 失败,如何获取一些消息
- r - 特殊字符在 renderUI 中不起作用
- google-apps-script - 更新一张谷歌表格上的代码,而无需在其他表格上更新