php “date():依赖系统的时区设置是不安全的......”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16765158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"date(): It is not safe to rely on the system's timezone settings..."
提问by user1852837
I got this error when I requested to update the PHPversion 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>
回答by CtrlX
You probably need to put the timezone in a configuration line in your php.ini
file. You should have a block like this in your php.ini file:
您可能需要将时区放在php.ini
文件的配置行中。你的 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.
这是支持的时区列表。
回答by Babatunde Adeyemi
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.
回答by Justis Matotoka
Add the following in your index.php
file. I first came across this when I moved my application from my XAMPPserver to Apache 2.2 and PHP 5.4...
在您的index.php
文件中添加以下内容。当我将我的应用程序从我的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');
}
回答by Susan Taunton
<? 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)
为我工作(显示当前年份,不再显示错误消息)。(感谢上面的克里斯)
回答by Chris
If these are not your options
如果这些不是您的选择
- Modifying
php.ini
. - Adding
date_default_timezone
call.
- 修改
php.ini
. - 添加
date_default_timezone
呼叫。
Instead of date
you could use gmdate
.
而不是date
你可以使用gmdate
.
I have used gmdate( "Y" )
when I needed a year for a copyright snipplet.
gmdate( "Y" )
当我需要一年的版权片段时,我已经使用过。
回答by Jose
If you are using CodeIgniter and can't change php.ini, I added the following to the beginning of index.php:
如果您使用的是 CodeIgniter 并且无法更改 php.ini,我在 index.php 的开头添加了以下内容:
date_default_timezone_set('GMT');
回答by Shaiful Islam
I always keep this line inside codeigniter's root index.php
.So that my code works at any server
我总是将这一行放在 codeigniter 的根目录中index.php
。这样我的代码就可以在任何服务器上运行
date_default_timezone_set('Asia/Dhaka');
回答by John Linhart
@Justis pointed me to the right direction, but his code did not work for me. This did:
@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');
}
Documentation: http://www.php.net/manual/en/function.date-default-timezone-get.php
文档:http: //www.php.net/manual/en/function.date-default-timezone-get.php
This solution is not only for those who does not have full system access. It is necessary for any script when you provide it to anyone else but you. You never know on what server the script will run when you distribute it to someone else.
此解决方案不仅适用于没有完整系统访问权限的人。当您将任何脚本提供给除您之外的任何其他人时,它对于任何脚本都是必要的。当您将脚本分发给其他人时,您永远不知道脚本将在哪个服务器上运行。
回答by nocturnalact
I had to put it in double quotes.
我不得不把它放在双引号中。
date_default_timezone_set("America/Los_Angeles"); // default time zone
回答by T0mmyKn1fe
This issue has been bugging me for SOME time as im trying to inject a "createbucket.php" script into composer and i keep being told my time-zone is incorrect.
这个问题一直困扰着我一段时间,因为我试图将“createbucket.php”脚本注入作曲家,但我一直被告知我的时区不正确。
In the end the only thing that fixed the issue was to:
$ sudo nano /etc/php.ini
最后,解决问题的唯一方法是:
$ sudo nano /etc/php.ini
Search for timezone
搜索时区
[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
Ensure you remove the ;
确保您删除 ;
Then finally
然后最后
$ sudo service httpd restart
And you'll be good to go :)
你会很高兴的:)