php PHP错误的日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8364323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP wrong date/time
提问by relic
PHP date()
& time()
return incorrect time:
PHP date()
&time()
返回错误的时间:
When date.timezone = "Europe/Riga"
the time returned by date()
was 03-12-2011 08:57:12
, but system time was 03-12-2011 01:57:12
(timezone Europe/Riga - correct time at that moment).
When I changed timezone to "Europe/London", the time changed to 03-12-2011 06:57:12
( actual time 02-12-2011 23:57:12
)
当date.timezone = "Europe/Riga"
返回的时间date()
是03-12-2011 08:57:12
,但系统时间是03-12-2011 01:57:12
(时区欧洲/里加 - 当时的正确时间)。当我将时区更改为“欧洲/伦敦”时,时间更改为03-12-2011 06:57:12
(实际时间02-12-2011 23:57:12
)
Time returned by date
/ hwclock --show
was correct (03-12-2011 01:57:12
with system timezone set as Riga)
date
/返回的时间hwclock --show
是正确的(03-12-2011 01:57:12
系统时区设置为里加)
OS: Debian 6.0
操作系统:Debian 6.0
I have checked most of the questions regarding similar issues on SO/Google, but they all seem to have wrong timezone specified.
我已经在 SO/Google 上检查了大多数关于类似问题的问题,但它们似乎都指定了错误的时区。
As far as I can tell there is problem between php -> os. Of course, because the incorrect time offset is always constant I could subtract difference, but it is not a proper solution.
据我所知,php -> os. 之间存在问题。当然,由于不正确的时间偏移始终是恒定的,我可以减去差异,但这不是一个正确的解决方案。
Any ideas will be greatly appreciated.
任何想法将不胜感激。
采纳答案by Arie Skliarouk
The problem looks similar to what I have seen on one of my servers. Looks to like bug in php 5.3.2-1. Try to run the php script in the bug report and post your results.
该问题看起来与我在其中一台服务器上看到的类似。看起来像php 5.3.2-1 中的错误。尝试运行错误报告中的 php 脚本并发布您的结果。
回答by Marco Pace
Reading PHP manual seems that behaviour of date.timezoneis affected by settings in php.ini. There is another way to set the default timezone in all date/time function and it's the date_default_timezone_set. Try to set it with:
阅读 PHP 手册似乎date.timezone 的行为受 php.ini 中的设置影响。还有另一种方法可以在所有日期/时间函数中设置默认时区,它是date_default_timezone_set。尝试设置它:
date_default_timezone_set('Europe/Riga');
instead of your date.timezonecode.
而不是您的date.timezone代码。
回答by user2317245
The timezone of the system could be wrong. This results in a shift in a time given by the PHP date() function, althought both php date.timezone (in php.ini) and system time of the server are correct.
系统的时区可能是错误的。这会导致 PHP date() 函数给出的时间发生变化,尽管 php date.timezone(在 php.ini 中)和服务器的系统时间都是正确的。
回答by Piotr
I had some problem with timezone too. This can be handy for someone.
我也有一些时区问题。这对某人来说可能很方便。
In my case, Chile Sumer Time CLST returned wrong timzeone offset.
就我而言,智利苏美尔时间 CLST 返回了错误的 timzeone 偏移量。
Updating the timezonedbworked for me.
更新timezonedb对我有用。
Go to: https://pecl.php.net/package/timezonedb
前往:https: //pecl.php.net/package/timezonedb
For WidnowsDownload the newest version of dll, copy into "ext" directory. Edit the php.ini and put below line:
对于Widnows下载最新版本的dll,复制到“ext”目录。编辑 php.ini 并将其放在以下行中:
extension=php_timezonedb.dll
扩展名=php_timezonedb.dll
For LinuxYou can use :
对于 Linux,您可以使用:
pecl install timezonedb
pecl 安装 timezonedb
and in php.ini put:
并在 php.ini 中输入:
extension=php_timezonedb.so
扩展= php_timezonedb.so
回答by Turan Zamanl?
This comment for Azerbaijan timezone
此评论适用于阿塞拜疆时区
I have problem with 'Asia/Baku' on php 7.2 or php 7.1 version.
我在 php 7.2 或 php 7.1 版本上遇到“亚洲/巴库”问题。
I solved problem change to 'Europe/Baku'
我解决了更改为“欧洲/巴库”的问题
回答by Mehdi Izcool
You have to set date.timezone in php.ini and restart your server http://php.net/manual/en/timezones.php
您必须在 php.ini 中设置 date.timezone 并重新启动服务器 http://php.net/manual/en/timezones.php
回答by Eddie
PHP time is based on epoch time scale which uses GMT and later UTC. Most people now refer to it as unix time. Because PHP uses unix time, timezones are not used. I believe subtracting the timezone hours in seconds is the correct method to adjust for the differences.
PHP 时间基于使用 GMT 和后来的 UTC 的纪元时间刻度。大多数人现在将其称为 unix 时间。因为 PHP 使用 unix 时间,所以不使用时区。我相信以秒为单位减去时区小时数是调整差异的正确方法。