开发者

How to correct php date-time problem

im having a problem with my datetime because when i use the php date() it is 1hr advance to my file or mysql server.

i have checked the server time using ssh and the result is the same from my mysql.

$curdatetime = date("Y-m-d H:i:s");  //<--- 1hr advance
$sqlqt = 开发者_如何学编程'SELECT NOW() AS time_rs';  //<--- correct result

could this be a server setting?.

Test results (Minutes ago when i posted the question): date():2011-04-17 06:18:09

MySQL:2011-04-17 05:18:09 //<-- the same as the ssh

Thanks.


Yes, it is caused by the server settings.

Start by checking out what date_default_timezone_get returns. Then check out the sources for the timezone in order of preference (they are given in the documentation) until you find where the value comes from.

Finally, either change that setting or use date_default_timezone_set to set the timezone in your script if you don't want to mess with global settings.


Your servers may have different time zones.

You can 'fix' this by changing the time zone of your servers.

The best solution is to use only UTC time instead of local time.

MySql and UTC:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_utc-time

PHP and UTC:

http://php.net/manual/en/function.gmdate.php


Just tossing my 2 cents in here...

I had the same problem ( an offset of 2 hours ) but my timezone was set right. I've finally fixed the problem with following code. I hope that this will help somebody in the future...

echo gmdate("Y/m/d H:i:s") . "<br />";
$timestamp = time()+date("Z");
echo gmdate("Y/m/d H:i:s",$timestamp);

That will output:

2014/07/01 09:57:20
2014/07/01 11:57:20
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜