php 时区:DateTime::createFromFormat 忽略时区

标签 php datetime

我在 debian 8 上使用 php 5.6.17。设置 owncloud 后,我注意到,
由 owncloud(不是由 apache)自行编写的日志具有错误的时区。

我对此进行了一些调查,似乎该行:

DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""), 'Europe/Berlin');

这不关心任何时区设置。而不是欧洲/柏林 (+1/+2) 的时间,我总是得到 UTC 的时间。

/etc/php5/apache2/php.ini我设置了"date.timezone = "Europe/Berlin"并且系统时间(debian)也是正确的。

即使我运行类似下面的东西,我也会得到相同的输出(UTC):
 $time=DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), new DateTimeZone('UTC'));
 echo $time->format('c') . "\n";
 $time=DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), new DateTimeZone('Europe/Berlin'));
 echo $time->format('c') . "\n";

关于这个问题的任何想法?

最佳答案

引用 PHP manual on DateTime::createFromFormat() :

Note: The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).



如果要更改时区,则必须使用 setTimezone()创建对象后的方法。

关于php 时区:DateTime::createFromFormat 忽略时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200360/

相关文章:

php - Laravel - 删除整个集合

java - 任何与时间戳一起使用的高性能 Java 库?

mysql - 如何在查询中向 MYSQL 中的日期添加天数

javascript - 在 JavaScript 中检查日期是否在给定年份的 1 月 1 日之前

python - 用 Pandas 按日期范围分组

sql-server - Microsoft SQL Server 如何比较查询语句中的 datetime 和 datetimeoffset 值?

javascript - SyntaxError : JSON. 解析:AJAX 调用中的意外字符错误

php - mysql查询的意外输出

php - 最新的 WP 在 PHP7 中破坏了 CSS

php - 什么是自动加载;如何使用 spl_autoload、__autoload 和 spl_autoload_register?