Heim  >  Artikel  >  php教程  >  PHP中设置时区方法总结

PHP中设置时区方法总结

WBOY
WBOYOriginal
2016-06-08 17:24:271029Durchsuche

使用php的朋友会发现如果你刚安装好apache与php环境,会发现你系统的时间与我们中国时间是有区别的,一般情况是相隔8小时哦,下面我来介绍PHP设置时区设置方法总结。

<script>ec(2);</script>

方法一,在php页面设置时区

在页面最前面加上下面应用一种都是可以的

date_default_timezone_set用法如下
date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set -- 设定用于一个脚本中所有日期时间函数的默认时区
说明
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() 设定用于所有日期时间函数的默认时区。


用法

 代码如下 复制代码

1 date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai'   亚洲/上海

2 date_default_timezone_set('Asia/Chongqing');//其中Asia/Chongqing'为“亚洲/重庆”

3 date_default_timezone_set('PRC');//其中PRC为“中华人民共和国”

4 ini_set('date.timezone','Etc/GMT-8');

5 ini_set('date.timezone','PRC');

6 ini_set('date.timezone','Asia/Shanghai');

7 ini_set('date.timezone','Asia/Chongqing');

Asia/Shanghai – 上海
Asia/Chongqing – 重庆
Asia/Urumqi – 乌鲁木齐
Asia/Hong_Kong – 香港
Asia/Macao – 澳门
Asia/Taipei – 台北
Asia/Singapore – 新加坡


如果你有服务器管理权限,我们可以在在PHP.INI中设置时区

 代码如下 复制代码

date.timezone = PRC

去掉前面的分号 然后重启apache

可能碰到的一些问题

1.Warning: strftime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

2.Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

3.Notice: date_default_timezone_set(): Timezone ID 'Asia/Shanghai' is invalid

解决办法

 代码如下 复制代码

date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' 亚洲/上海

date_default_timezone_set('Asia/Chongqing');//其中Asia/Chongqing'为“亚洲/重庆”

date_default_timezone_set('PRC');//其中PRC为“中华人民共和国”

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn