Home >Backend Development >PHP Tutorial >php timeZone设置和他影响的函数
php设置 timeZone方法
1,函数ini_set()设置时区:
可以在文件开头加入 ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai' 为上海时区
2,date_default_timezone_set(‘Asia/Shanghai');
3,手动修改php.ini设置
打开php找到date.timezone = "PRC" 如有去掉前面的分号,没有的话手动添加!
date()
strtotime()
timeZone设置不影响的函数
time()
gmdate()
测试数据<?php$time = time();$value = 'PRC';date_default_timezone_set($value);echo date('Y-m-d H:i:s',$time),"\n";echo gmdate('Y-m-d H:i:s',$time),"\n";var_dump(date_default_timezone_get());$value = 'Asia/Chongqing';date_default_timezone_set($value);echo date('Y-m-d H:i:s',$time),"\n";echo gmdate('Y-m-d H:i:s',$time),"\n";var_dump(date_default_timezone_get());$value = 'Asia/Shanghai';date_default_timezone_set($value);echo date('Y-m-d H:i:s',$time),"\n";echo gmdate('Y-m-d H:i:s',$time),"\n";var_dump(date_default_timezone_get());$value = 'Europe/Riga';date_default_timezone_set($value);echo date('Y-m-d H:i:s',$time),"\n";echo gmdate('Y-m-d H:i:s',$time),"\n";var_dump(date_default_timezone_get());?>
2015-10-12 12:13:512015-10-12 04:13:51string(3) "PRC"2015-10-12 12:13:512015-10-12 04:13:51string(14) "Asia/Chongqing"2015-10-12 12:13:512015-10-12 04:13:51string(13) "Asia/Shanghai"2015-10-12 07:13:512015-10-12 04:13:51string(11) "Europe/Riga"