Home > php教程 > php手册 > php中date函数的时区输出时间有误

php中date函数的时区输出时间有误

WBOY
Release: 2016-06-13 09:56:51
Original
1020 people have browsed it

在php中如果我们不作任何设置一般情况下利用date输出时间是会有误差的,而且很大。

问题:echo date('o-m-j H:i');

 代码如下 复制代码

〈?php
         echo date(’Y-m-d H:i:s’);
  ?〉


输出当前时间:2008-10-12 02:32:17
怪了,实际时间是:2008-10-12 10:32:17

输出结果与本地时间相差8小时。
原因:时区未正确设置。
两种解决方法:

1.加入date_default_timezone_set('Asia/Shanghai');
2. 修改php.ini

[Date]

; Defines the default timezone used by the date functions
date.timezone = PRC 注:PRC为中化人民共和国

如果没有修改php.ini的权限,那么应该在调用date()方法之前加上

 代码如下 复制代码
date_default_timezone_set(‘PRC’);
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template