本文翻译自:“date(): It is not safe to rely on the system's timezone settings…”
I got this error when I requested to update the PHP version from 5.2.17 to PHP 5.3.21 on the server. 当我请求将服务器上的PHP版本从5.2.17更新到PHP 5.3.21时,出现此错误。
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: 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 'America/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 86</p>
</div>
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 'America/New_York' for 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 86
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 'America/New_York' for 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 99
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: 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 'America/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 99</p>
</div>
#1楼
参考:https://stackoom.com/question/18LNm/date-依靠系统的时区设置是不安全的
#2楼
You probably need to put the timezone in a configuration line in your php.ini
file. 您可能需要将时区放在php.ini
文件的配置行中。 You should have a block like this in your php.ini file: 您应该在php.ini文件中有一个这样的块:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York
If not, add it (replacing the timezone by yours). 如果没有,请添加它(用您的时区代替)。 After configuring, make sure to restart httpd ( service httpd restart
). 配置后,请确保重新启动httpd( service httpd restart
)。
Here is the list of supported timezones . 这是受支持的时区列表 。
#3楼
If you cannot modify your php.ini configuration, you could as well use the following snippet at the beginning of your code: 如果您无法修改php.ini配置,则还可以在代码开头使用以下代码段:
date_default_timezone_set('Africa/Lagos');//or change to whatever timezone you want
The list of timezones can be found at http://www.php.net/manual/en/timezones.php . 时区列表可以在http://www.php.net/manual/en/timezones.php中找到。
#4楼
If these are not your options 如果这些不是您的选择
- Modifying
php.ini
. 修改php.ini
。 - Adding
date_default_timezone
call. 添加date_default_timezone
调用。
Instead of date
you could use gmdate
. 可以使用gmdate
代替date
。
I have used gmdate( "Y" )
when I needed a year for a copyright snipplet. 当我需要一年的版权摘要时,我使用了gmdate( "Y" )
。
#5楼
Add the following in your index.php
file. 在index.php
文件中添加以下内容。 I first came across this when I moved my application from my XAMPP server to Apache 2.2 and PHP 5.4... 当我将我的应用程序从XAMPP服务器移到Apache 2.2和PHP 5.4时,我第一次遇到了这个问题。
I would advise you do it in your index.php
file instead of the php.ini
file. 我建议您在index.php
文件而不是php.ini
文件中执行此操作。
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}
#6楼
<? print(gmdate("Y")); ?>
instead of 代替
<? print(date("Y")); ?>
worked for me (shows current year and no more shows the error message). 为我工作(显示当前年份,并且不再显示错误消息)。 (Thanks to Chris above) (感谢上面的克里斯)