php Symfony2 和 date_default_timezone_get() - 依赖系统的时区设置是不安全的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20743060/
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
Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings
提问by Milo?
I have a Symfony2 project. I updated my php to 5.5.7 today and since then, I am getting the
我有一个 Symfony2 项目。我今天将我的 php 更新为 5.5.7,从那时起,我得到了
Warning: date_default_timezone_get(): 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...
I setup the default timezone in my php.ini
我在 php.ini 中设置了默认时区
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Paris";
To be sure that this is the good php.ini, I was checking with
为了确保这是好的 php.ini,我正在检查
phpinfo();
And the path I ma getting there is the one I am modifying:
我到达那里的路径是我正在修改的路径:
/usr/local/php5.5.7/lib
But in there, I see the
但在那里,我看到
Default timezone UTC
Which is strange.
这很奇怪。
any idea? Thank you.
任何的想法?谢谢你。
回答by sas
Maybe you are trying to set it in Apache's php.ini, but your CLI (Command Line Interface) php.iniis not good.
也许您正在尝试在 Apache 中设置它php.ini,但是您的 CLI(命令行界面)php.ini不好。
Find your php.inifile with the following command:
php.ini使用以下命令查找您的文件:
php -i | grep php.ini
And then search for date.timezoneand set it to "Europe/Amsterdam". all valid timezone will be found here http://php.net/manual/en/timezones.php
然后搜索date.timezone并将其设置为"Europe/Amsterdam". 所有有效的时区都可以在这里找到http://php.net/manual/en/timezones.php
Another way (if the other does not work), search for the file AppKernel.php, which should be under the folder appof your Symfonyproject directory. Overwrite the __constructfunction below in the class AppKernel:
另一种方法(如果其他方法不起作用),搜索文件AppKernel.php,它应该在app您的Symfony项目目录的文件夹下。覆盖__construct类中的以下函数AppKernel:
<?php
class AppKernel extends Kernel
{
// Other methods and variables
// Append this init function below
public function __construct($environment, $debug)
{
date_default_timezone_set( 'Europe/Paris' );
parent::__construct($environment, $debug);
}
}
回答by Peter
Found a similar way to fix this issue (at least it did for me).
找到了解决此问题的类似方法(至少对我来说是这样)。
First check where the
CLI php.iniis located:php -i | grep "php.ini"In my case I ended up with : Configuration File (php.ini) Path => /etc
Then
cd ..all the way back andcdinto/etc, dolsin my case php.ini didn't show up, only a php.ini.defaultNow, copy the php.ini.default file named as php.ini:
sudo cp php.ini.default php.iniIn order to edit, change the permissions of the file:
sudo chmod ug+w php.inisudo chgrp staff php.iniOpen directory and edit the php.ini file:
open .Tip: If you are not able to edit the php.ini due to some permissions issue then copy 'php.ini.default' and paste it on your desktop and rename it to 'php.ini' then open it and edit it following step 7. Then move (copy+paste) it in /etc folder. Issue will be resolved.
Search for [Date] and make sure the following line is in the correct format:
date.timezone = "Europe/Amsterdam"
首先检查
CLI php.ini位置:php -i | grep "php.ini"就我而言,我最终得到了:配置文件 (php.ini) 路径 => /etc
然后
cd ..所有的回道和cd进/etc,做ls我的情况的php.ini没有露面,只有php.ini.default现在,复制名为 php.ini 的 php.ini.default 文件:
sudo cp php.ini.default php.ini为了编辑,更改文件的权限:
sudo chmod ug+w php.inisudo chgrp staff php.ini打开目录并编辑 php.ini 文件:
open .提示:如果由于某些权限问题您无法编辑 php.ini,请复制“php.ini.default”并将其粘贴到您的桌面上并将其重命名为“php.ini”,然后打开它并按照以下步骤进行编辑7. 然后将其移动(复制+粘贴)到 /etc 文件夹中。问题将得到解决。
搜索 [Date] 并确保以下行的格式正确:
date.timezone = "Europe/Amsterdam"
I hope this could help you out.
我希望这可以帮助你。
回答by Krzysztof Bociurko
The current accepted answer by crack is deprecated in Symfony 2.3 and will be removed by 3.0. It should be moved to the constructor:
目前接受的破解答案在 Symfony 2.3 中已被弃用,并将在 3.0 中删除。它应该移到构造函数中:
public function __construct($environment, $debug) {
date_default_timezone_set('Europe/Warsaw');
parent::__construct($environment, $debug);
}
回答by Valentas
Since PHP 5.5, there is a separate php.ini file for CLI interface. If You use symfony console from command line, then this specific php.ini is used.
从 PHP 5.5 开始,CLI 界面有一个单独的 php.ini 文件。如果您从命令行使用 symfony 控制台,则使用这个特定的 php.ini。
In Ubuntu 13.10 check file:
在 Ubuntu 13.10 检查文件:
/etc/php5/cli/php.ini
/etc/php5/cli/php.ini
回答by bilelovitch
The problem appear when we are using PHP 5.1 on Redhat or Cent OS
当我们在 Redhat 或 Cent OS 上使用 PHP 5.1 时出现问题
PHP 5.1 on RHEL/CentOS doesn't support the timezone functions
RHEL/CentOS 上的 PHP 5.1 不支持时区函数
回答by cxw
Following up on sas's answer, PHP 5.4, Symfony 2.8, I had to use
跟进sas 的回答,PHP 5.4,Symfony 2.8,我不得不使用
ini_set('date.timezone','<whatever timezone string>');
instead of date_default_timezone_set. I also added a call to ini_setto the top of a custom web/config.phpto get that check to succeed.
而不是date_default_timezone_set. 我还添加了对ini_set自定义顶部的调用,web/config.php以使该检查成功。
回答by Sifb71
add this code to Your AppKernel Class:
将此代码添加到您的 AppKernel 类:
public function init()
{
date_default_timezone_set('Asia/Tehran');
parent::init();
}

