使用php的朋友会发现如果你刚安装好apache与php环境,会发现你系统的时间与我们中国时间是有区别的,一般情况是相隔8小时哦,下面我来介绍PHP设置时区设置方法总结。
方法一,在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为“中华人民共和国” |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
