首页 \ 问答 \ php setTimezone同时出现不同的结果(php setTimezone different result for the same time)

php setTimezone同时出现不同的结果(php setTimezone different result for the same time)

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

测试代码:

# 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

I'm getting different timezone offsets for the same time on two different machines. I cannot find root cause for that.

Testing code:

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

The result I get on one machine is:

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"
}

But on another:

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"
}

Istanbul is 3 hours ahead of UTC on November 6 (daylight changes are on Nov 8 and Mar 29). Things I tried:

  • changing system time (different months in the year)
  • diff of php -i, env variables (no other changes than SSH_CLIENT, SSH_TTY, SSH_AUTH_SOCK, SSH_CONNECTION environment variables)

other info (on both machines is the same php binary (the same 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

原文:https://stackoverflow.com/questions/34310895
更新时间:2020-10-25 14:10

最满意答案

我想,1小时的差异表明了夏令时问题。 - 检查两台计算机上的系统时区和夏令时设置 - 在两台计算机上检查php.ini中的时区和夏令时


I find out that php depends on tzdata package, which received an update (changelog http://changelogs.ubuntu.com/changelogs/pool/main/t/tzdata/tzdata_2015g-0ubuntu0.12.04/changelog).

So after doing apt-get install tzdata both machines provided the same result.

2016-01-08

相关文章

更多

最新问答

更多
  • 如何保护Solr只允许SELECT请求给用户并禁止其他任何东西?(How to secure Solr to allow SELECT request to only users and disallow anything else?)
  • XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)
  • css3 3D变换不能平滑地制作动画(css3 3D transform doesn't animate smoothly)
  • 运行时错误'91'和Outlook.Application = <对象变量或没有设置块变量>?(Run-time error '91' & Outlook.Application = Object variable or With block variable not set?)
  • 慢的webservice问题(Slow webservice problem)
  • textview的不正确对齐方式(Improper alignment of a textview)
  • 在第一步“Hello World”中出现Java错误(Cannot run “Hello World” program in Eclipse)
  • 为什么十六进制地址是14个字符?(why the hex address is 14 character?)
  • 如何在Python中的不同类中使用变量?(How to use variable in different classes in Python?)
  • asp:GridView HYPERLINKFIELD - datanavigateurlformatstring中的asp代码(asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring)
  • 关于adaboost算法(About adaboost algorithm)
  • 在Matlab上内置图像(Built in Images on Matlab)
  • java swing:输入键事件时焦点丢失(java swing: Focus lost on enter key event)
  • C#中的通用约束,T是相同的TSomethingElse,对吧?(Generic constraints in C#, T is the same TSomethingElse, right?)
  • 从mybatis中的光标获取数据(Fetching data from cursor in mybatis)
  • 在运行时从XML构建对象的最佳方法(Best way for building objects out of XMLs at runtime)
  • 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)
  • Selenium Node API Web驱动程序等待超时处理程序(Selenium Node API web driver wait timeout handler)
  • 函数使用并且是map的一部分(循环依赖?)(Function uses and is part of map (circular dependency?))
  • 是否可以在C ++ 14中使用可选模板参数创建类型元组?(Is possible to make a tuple of types with optional template parameters in C++14?)
  • PHP从窗帘后面打印/ f(PHP prints /f from behind the curtains)
  • JFrame的contentPane的LayoutManager(LayoutManager of JFrame's contentPane)
  • 用于Instagram Feed的图像滚动(Image Roll Overs for Instagram Feed)
  • 如何显示拉伸字体(双倍宽度/高度)?(How to display stretched font (double width/height)?)
  • 文件操作API在fileapi.h和stdio.h中的WP8差异?(WP8 differences between file manipulation APIs in fileapi.h and stdio.h?)
  • 保存失败后Rails没有回滚事务()(Rails not rolling back transaction after failed save())
  • jqgrid中的分页问题与数组数据(Pagination problem in jqgrid with array data)
  • 重定向时,通过其他页面上的URL调用javascript函数(Call javascript function through url on otherpage while Redirecting)
  • 如何使用InvokeCommandAction调用我的方法并传入参数?(How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?)
  • Jquerymobile按钮仅显示页面的第一个外观(Jquerymobile buttons are shown only first apperance of the page)