php date_default_timezone_set('UTC') 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11770730/
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
date_default_timezone_set('UTC') not working
提问by Netorica
This seems to be weird but I already check everything, and still a weird thing happens.
这似乎很奇怪,但我已经检查了所有内容,但仍然发生了奇怪的事情。
I can't change the timezone of my php scripts.
我无法更改 php 脚本的时区。
First things first: what I did was something like this:
第一件事:我所做的是这样的:
<?php
date_default_timezone_set('UTC');
echo '<br>';
echo date('Y-m-d H:i:s');
?>
this seems to be working fine when I tried this on a test http://codepad.org/rpYZ0flA.
当我在测试http://codepad.org/rpYZ0flA上尝试这个时,这似乎工作正常。
My server's timezone is set to UTC+8:00 Taipei, but when I tried the code above it's not really working. It still shows my current date_time in my server's timezone, not following the code above.
我服务器的时区设置为 UTC+8:00 台北,但是当我尝试上面的代码时,它并没有真正起作用。它仍然在我的服务器时区中显示我当前的 date_time,而不是按照上面的代码。
And this is the php.ini configuration of my server:
这是我服务器的 php.ini 配置:
date/time support enabled
"Olson" Timezone Database Version 2012.3
Timezone Database internal
Default timezone Europe/Berlin
Why this is happening? Is this already a bug? Or mistake on server_setup or I just missed something in my code?
为什么会发生这种情况?这已经是一个错误了吗?或者在 server_setup 上出错,或者我只是错过了我的代码中的某些内容?
Thank you.
谢谢你。
NOTE:My environment is a Windows 7N running in VM using PHP 5.4.4
注意:我的环境是使用 PHP 5.4.4 在 VM 中运行的 Windows 7N
FIX:
使固定:
I got the fix by changing manually the php.ini
我通过手动更改 php.ini 得到了修复
回答by sas
Try this
尝试这个
<?php
echo date('Y-m-d H:i:s T', time()) . "\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time()) . "\n";
here you will find the test result http://codepad.org/gc5oYnLW
在这里你会找到测试结果http://codepad.org/gc5oYnLW
回答by Rafael Keller
If you want only the time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) with timezone. Use the code below:
如果您只想要带时区的 Unix 纪元(1970 年 1 月 1 日 00:00:00)以来的时间(以秒为单位)。使用下面的代码:
<?php
date_default_timezone_set("UTC");
time()+date("Z");
回答by e-sushi
It should work without any problems.
它应该可以正常工作。
In case of doubt, check it with this code:
如有疑问,请使用以下代码检查:
<?php
date_default_timezone_set('America/Virgin');
echo date('Y-m-d H:i:s T') . "\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T') . "\n";
Here is a codepad previewfor your convenience.
为了您的方便,这里有一个键盘预览。
The return will look like this (with updated date and time off course):
返回将如下所示(更新日期和时间偏离课程):
2017-12-11 03:09:58 AST
2017-12-11 07:09:58 UTC
If that fails, double-check your server configuration… starting with your PHP.inifile.
如果失败,请仔细检查您的服务器配置……从您的PHP.ini文件开始。

