PHP设置时区函数:date_default_timezone_set()
作者:Alpha时间:2017-09-14 阅读数:3531 +人阅读
在进行时间处理时,我们首先要做的就是设置好时区,不然处理的结果和我们想要的会有比较大的差距:
//这是格林威治标准时间,得到的时间和默认时区是一样的
echo date_default_timezone_set("Etc/GMT")."<br>";
echo date('Y-m-d H:i:s')."<br>";
//这里比林威治标准时间慢8小时
echo date_default_timezone_set("Etc/GMT+8")."<br>";
echo date('Y-m-d H:i:s')."<br>";
//由上一个不能难想像,我们比那快8小时所以减8
echo date_default_timezone_set("Etc/GMT-8")."<br>";
echo date('Y-m-d H:i:s')."<br>";
//设置中国时区
echo date_default_timezone_set('PRC')."<br>";
echo date('Y-m-d H:i:s')."<br>";//中国标准时间
在未设置时区时,使用date()函数时会有warning提示: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. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in D:\phpStudy\WWW\learn.php.com\time\age.php on line 9
输出结果:
1 2017-04-27 14:29:51 1 2017-04-27 06:29:51 1 2017-04-27 22:29:51 1 2017-04-27 22:29:51
对我们比较实用的一个就是设置中国时区:date_default_timezone_set('PRC');这样设置后,再输出时间就和我们当前的时间一直了。
本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:595397166@qq.com
上一篇:没有了
下一篇:php根据出生日期计算年龄函数