PHP5时区问题
发布于 2007-02-28 17:42 4 阅读:13,307 评论:4 标签: php 时区

今天在PHP5下用date("H:i:s")时,发现参数"H"取出的时间与window下的时间不对。查了一下资料,发现是PHP5的php.ini里面默认设置为:

[Date]
; Defines the default timezone used by the date functions
;date.timezone =

如此一来,按照默认的时间便为GMT时间。而我们一般是使用北京时间,可以设置为:date.timezone = Asia/Shanghai。即:

[Date]
; Defines the default timezone used by the date functions
date.timezone = Asia/Shanghai

记得不要设置为"Asia/Beijing",老外好象对上海感兴趣点,呵呵。

如果没有权限改php.ini,可以用函数date_default_timezone_set('Asia/Shanghai');

这个函数用于设定所有日期时间函数的默认时区。手册上如此说明:“自 PHP 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 E_NOTICE 级别的错误信息”。但是“本函数永远返回 TRUE(即使 timezone_identifier 参数不合法)。”

在此再学习一下函数:string date_default_timezone_get ( void )。这个函数的目的是取得一个脚本中所有日期时间函数所使用的默认时区。

这个函数的返回值遵循以下顺序:1:用 date_default_timezone_set() 函数设定的时区(如果设定了的话)。2:TZ 环境变量(如果非空)。3:date.timezone 配置选项(如果设定了的话)。4:自己推测(如果操作系统支持)。5:如果以上选择都不成功,则返回 UTC

再深入学习一下什么是UTC:
协调世界时(UTC):
一种称为协调世界时的折衷时标于1972年面世。为了确保协调世界时与世界时(UT1)相差不会超过0.9秒,有需要时便会在协调世界时内加上正或负闰秒。因此协调世界时与国际原子时(TAI)之间会出现若干整数秒的差别。位于巴黎的国际地球自转事务中央局(IERS)负责决定何时加入闰秒。

UTC = Coordinated Universal Time. 中文名称为协调世界时.

GMT = Greenwich Mean Time. 中文名称为格林尼治(平)时(这里的"w"是不发音的,而且"Green"要读成"Gren")

UTC = GMT +/- 0.9 s
因此 UTC 间中需要进行 "闰秒" 以控制两者相差。

但是TZ 环境变量呢?还没找到详细的资料......

附参考资料:
1:请问在windows下安装Apache服务器的时间怎么调整?
2:什么是GMT,什么是UTC
3:谁能详细解释一下UTC和GMT

to yayu

起了啊..还是不行...

by ihwm 2007-03-24 20:21:13
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
to ihwm

你重启Apache了吗?

如果是,那偶也不知道了

by yayu 2007-03-24 17:35:54
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
why

为什么我的也是显视的不一样...改PHP.INI后还是不行...请指教...thank you

by ihwm 2007-03-23 23:37:51
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
有意思

记得不要设置为"Asia/Beijing",老外好象对上海感兴趣点,呵呵

有意思,老外经济的热情应该高点~

by 彳亍 2007-03-02 17:02:48
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172