PHP5时间相差八小时问题[三种方法]

from:http://show.xoyo.com/php/article/135713.shtml

打开php.ini,的确有一项:
[Date]
; Defines the default timezone used by the date functions
;date.timezone =
以前的版本是没有的,在官方网站上文档找到相关内容,默认date.timezone为utc,在网站上有一个时区对照表:http://www.php.net/manual/en/timezones.asia.php,这个是亚洲的。竟然没有北京,有重庆、香港。我们选择香港设置......设置后为:

方法1:
找到php.ini中的“;date.timezone =”这行,将“;”去掉,改成“date.timezone = PRC”(PRC:People's Republic of China 中华人民共和国),重启Apache,问题解决。

方法2:
在php5以及起以上的版本,要输出本地的时间(限中国),可以这么写代码:

<?php
date_default_timezone_set('Asia/Shanghai');
echo date('Y-m-d H:i:s');
?>

也可以这样写代码:

<?php
date_default_timezone_set('Asia/Chongqing');
echo date('Y-m-d H:i:s');
?>

这样时间相差八小时的问题就解决了!!~~~

-----------------------------
[Date]
; Defines the default timezone used by the date functions
date.timezone = Asia/Hong_Kong
重启后,成功,问题解决。

或:


代码:

<?php
/*
@title\:PHP5.1时区问题
@author:axgle
@contents:与php.ini中date.timezone ="Hongkong"等效
*/
//header('refresh:1');
if(function_exists('date_default_timezone_set')) {
date_default_timezone_set('Hongkong');//该函数为PHP5.1内置.
echo date_default_timezone_get().'
';
}
echo date("Y-m-d H:i:s");
?>

posted @ 2008-07-09 01:15  dkcndk  阅读(223)  评论(0编辑  收藏  举报