search
Homephp教程PHP源码PHP中设置时区方法总结

使用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为“中华人民共和国”

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.