Home >Backend Development >PHP Tutorial > Codeigniter TimeZone 时区有关问题及 PHP 时区设定

Codeigniter TimeZone 时区有关问题及 PHP 时区设定

WBOY
WBOYOriginal
2016-06-13 13:12:48880browse

Codeigniter TimeZone 时区问题及 PHP 时区设定

我使用 Codeigniter 框架来搭建网站,使用 date ('Y-m-d h:i:s') 来获取时间并存入数据库,但是今天发现,存入的时间和我本地时间差好多。


因为时区不对导致时间和中国时间不一致,解决办法一般有两种:


(1)修改php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC

修改后重启 apache即可。


(2)在使用date() 之前使用date_default_timezone_set("Asia/Shanghai")

中国地区有如下的参数可以设置,具体分类的方法有待考察。

{ "cst",   0,  28800, "Asia/Chongqing"                },
{ "cst",   0,  28800, "Asia/Chungking"                },
{ "cst",   0,  28800, "Asia/Harbin"                   },
{ "cst",   0,  28800, "Asia/Kashgar"                  },
{ "cst",   0,  28800, "Asia/Macao"                    },
{ "cst",   0,  28800, "Asia/Macau"                    },
{ "cst",   0,  28800, "Asia/Shanghai"                 },
{ "cst",   0,  28800, "Asia/Taipei"                   },
{ "cst",   0,  28800, "Asia/Urumqi"                   },
{ "cst",   0,  28800, "PRC"                           },
{ "cst",   0,  28800, "ROC"                           },

但是,在codeigniter中,使用date后,发现时间依然差好多,所以,codeigniter中一定有设置时区的配置。

查看发现: 文件 application/config/MY_config.php中有如下设置:

<?php date_default_timezone_set('America/Los_Angeles');

只要改为date_default_timezone_set("Asia/Shanghai")即可。

 
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