php setTimezone同时出现不同的结果

时间:2015-12-16 11:30:30

标签: php

我在两台不同的机器上同时获得不同的时区偏移量。我无法找到根本原因。

测试代码:

# create empty directory and file, so php has no configuration
mkdir /tmp/empty; touch /tmp/emptycfg
PHP_INI_SCAN_DIR=/tmp/empty /usr/bin/php5 -c /tmp/emptycfg -r '$curTime = new \DateTime("2015-11-06 12:59:59", new \DateTimeZone("UTC")); var_dump($curTime); $curTime->setTimezone(new \DateTimeZone("Europe/Istanbul")); var_dump($curTime);'

我在一台机器上得到的结果是:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-11-06 12:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-11-06 15:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "Europe/Istanbul"
}

但另一方面:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-11-06 12:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-11-06 14:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "Europe/Istanbul"
}

伊斯坦布尔11月6日比UTC早3小时(白天变化是11月8日和3月29日)。 我试过的事情:

  • 更改系统时间(一年中的不同月份)
  • php -i,env变量的差异(没有比SSH_CLIENT,SSH_TTY,SSH_AUTH_SOCK,SSH_CONNECTION环境变量更多的其他变化)

其他信息(在两台机器上都是相同的php二进制文件(相同的md5sum))

$ PHP_INI_SCAN_DIR=/tmp/empty /usr/bin/php5 -c /tmp/emptycfg -i | head -n 30
phpinfo()
PHP Version => 5.6.16-1+deb.sury.org~precise+1

System => Linux dev 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /tmp/emptycfg
Scan this dir for additional .ini files => /tmp/empty
Additional .ini files parsed => (none)
PHP API => 20131106
PHP Extension => 20131226
Zend Extension => 220131226
Zend Extension Build => API220131226,NTS
PHP Extension Build => API20131226,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => disabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => enabled

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

2 个答案:

答案 0 :(得分:0)

我认为,1小时差异显示出夏令时问题。 - 检查两台计算机上的系统时区和夏令时设置 - 在两台计算机上检查php.ini中的时区和夏令时

答案 1 :(得分:0)

我发现php依赖于tzdata包,它收到了更新(changelog http://changelogs.ubuntu.com/changelogs/pool/main/t/tzdata/tzdata_2015g-0ubuntu0.12.04/changelog)。

因此,在完成apt-get install tzdata后,两台机器都提供了相同的结果。