PHP5.2中date()函数显示时间与北京时间相差8小时的解决办法
问题:
php函数date("Y-n-d H-i-s"); 输出的时间与当地时间相差8个小时.
解决思路
北京时间是东8区,可能是时区问题.但是此前都没问题啊,经过google发现原来从php5.1.0开始,php.ini里加入了 date.timezone这个选项,默认情况下是关闭的,也就是显示的时间(无论用什么php命令)都是格林威治标准时间,和我们的时间(北京时间)差了正好8个小时。
在调用date()函数之前先设定时区。
方法一:
date_default_timezone_set('PRC');
没有返回值。下面再直接使用date函数就OK了
$time = date(Y."年".m."月".d."日".G."时".i."分");
方法二:
修改php.ini中的;date.timezone = 为date.timezone = "Asia/Shanghai"(或者改为date.timezone = PRC)并去掉前面的分号,重启Apache即可。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

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

Dreamweaver CS6
Visual web development tools