"date():依靠系统的时区设置是不安全的......"

use*_*837 351 php timezone

当我请求在服务器上将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>
Run Code Online (Sandbox Code Playgroud)

Ctr*_*rlX 548

您可能需要将时区放在php.ini文件的配置行中.你应该在php.ini文件中有一个这样的块:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York
Run Code Online (Sandbox Code Playgroud)

如果没有,请添加它(由您的时区替换).配置完成后,请确保重新启动httpd(service httpd restart).

以下是支持的时区列表.

  • 要明确,进行此更改后,请务必sudo service apache2 restart (12认同)
  • 如果您在客户端终端中使用PHP CLI(php -r或php -a),则需要编辑CLI php.ini文件,而不是apache2.您可以在/etc/php5/cli/php.ini找到YourC CLI php.ini文件 (9认同)
  • `apachectl graceful`就足够了(至少对我有用). (7认同)
  • 当其他解决方案没有时,这为我解决了.当我在与Amazon SES集成的同一页面上使用日期功能时,我只收到此错误.也许这会帮助别人. (2认同)
  • 与关于Apache的第一条评论一样,您还必须重新启动IIS才能使其在Windows/IIS上生效. (2认同)
  • 这解决了我的问题!只是像我这样的非专家的信息,为了找到apache使用的`php.ini`,只需执行以下命令`php -i | grep php.ini` (2认同)

Bab*_*emi 196

如果您无法修改php.ini配置,您也可以在代码的开头使用以下代码段:

date_default_timezone_set('Africa/Lagos');//or change to whatever timezone you want
Run Code Online (Sandbox Code Playgroud)

时区列表可在http://www.php.net/manual/en/timezones.php找到.

  • 出于某种原因,在我的php.ini中设置date.timezone = America/New_York并且在使用swiftmailer运行脚本时重新启动apache没有停止此错误消息.我最后还要添加date_default_timezone_set('America/New_York'); 在建议的脚本顶部.谢谢! (7认同)

小智 70

在您的index.php文件中添加以下内容.当我将我的应用程序从XAMPP服务器移动到Apache 2.2和PHP 5.4 时,我第一次遇到这个问题...

我建议你在你的index.php文件中而不是php.ini文件中进行.

if( ! ini_get('date.timezone') )
{
    date_default_timezone_set('GMT');
}
Run Code Online (Sandbox Code Playgroud)

  • 当您不一定拥有完整的系统访问权限时,可以选择一个优秀的解决方案,猜测这个解决方案适用于访问受限或需要更强大的解决方案的人,并将其与编辑php.ini相结合(如果可以的话) (3认同)

小智 18

<? print(gmdate("Y")); ?> 
Run Code Online (Sandbox Code Playgroud)

代替

<? print(date("Y")); ?>
Run Code Online (Sandbox Code Playgroud)

为我工作(显示当前年份,不再显示错误消息).(感谢克里斯上面)


Chr*_*ris 15

如果这些不是您的选择

  • 修改php.ini.
  • 添加date_default_timezone电话.

而不是date你可以使用gmdate.

gmdate( "Y" )当我需要一年的版权信息时,我已经习惯了.


小智 14

如果您正在使用CodeIgniter并且无法更改php.ini,我将以下内容添加到index.php的开头:

date_default_timezone_set('GMT');
Run Code Online (Sandbox Code Playgroud)


Sha*_*lam 13

我始终将这一行保留在codeigniter的根目录中index.php.因此我的代码可以在任何服务器上运行

date_default_timezone_set('Asia/Dhaka');
Run Code Online (Sandbox Code Playgroud)

此处支持的时区列表


Joh*_*art 11

@Justis向我指出了正确的方向,但他的代码对我不起作用.这样做:

// set the default timezone if not set at php.ini
if (!date_default_timezone_get('date.timezone')) {
    // insert here the default timezone
    date_default_timezone_set('America/New_York');
}
Run Code Online (Sandbox Code Playgroud)

文档:http://www.php.net/manual/en/function.date-default-timezone-get.php

此解决方案不仅适用于没有完整系统访问权限的用户.除了您之外,任何脚本都必须提供给任何其他人.当您将脚本分发给其他人时,您永远不知道脚本将在什么服务器上运行.


小智 5

我不得不把它放在双引号中。

date_default_timezone_set("America/Los_Angeles"); // default time zone
Run Code Online (Sandbox Code Playgroud)


小智 5

这个问题一直困扰着我一段时间,因为我试图将“ createbucket.php”脚本注入作曲家,而且我一直被告知时区不正确。

最后,解决该问题的唯一方法是: $ sudo nano /etc/php.ini

搜索时区

[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = UTC
Run Code Online (Sandbox Code Playgroud)

确保删除 ;

然后最后

$ sudo service httpd restart
Run Code Online (Sandbox Code Playgroud)

而且你会很好:)