我的php.ini文件中有这个条目:
date.timezone = 'Europe/London';
但每次我在命令行脚本中使用DateTime()时,我仍然会收到以下错误:
Exception: DateTime::__construct(): 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 'Europe/London' for 'BST/1.0/DST' instead
因此,我必须在任何脚本中执行以下操作才能使其工作:
date_default_timezone_set('Europe/London');
还有什么原因会导致此错误?
更新
我在命令行和web浏览器中都使用了以下内容:
<?php
var_dump(ini_get('date.timezone'));
exit;
在命令行中,我得到:
string(0) ""
在网络浏览器中我得到:
string(13) "Europe/London"
CLI和Web服务器(以及cgi和fpm)使用不同的php.ini
-文件。因为你说你在浏览器中看到了正确的值,我猜你编辑错了。关于cli类型
php --ini
要找到,您必须编辑哪个。它是第二行中的一个,例如
Loaded Configuration File: /etc/php5/cli/php.ini
一些服务器(如media temple)为特定域进行配置。因此,服务器将访问/etc/php.ini下的公共php.ini文件,但浏览器访问域的php.ini文件。