繁体   English   中英

Symfony2和date_default_timezone_get() - 依赖系统的时区设置是不安全的

[英]Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings

我有一个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...

我在php.ini中设置了默认时区

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Paris";

为了确保这是一个很好的php.ini,我正在检查

phpinfo();

我到达那里的路径是我正在修改的路径:

 /usr/local/php5.5.7/lib 

但在那里,我看到了

Default timezone    UTC 

这很奇怪。

任何的想法? 谢谢。

也许你正试图在Apache的php.ini设置它,但你的CLI(命令行界面) php.ini并不好。

使用以下命令查找php.ini文件:

php -i | grep php.ini

然后搜索date.timezone并将其设置为"Europe/Amsterdam" 所有有效的时区都可以在http://php.net/manual/en/timezones.php找到

另一种方法(如果另一种方法不起作用),搜索文件AppKernel.php ,该文件应位于Symfony项目目录的文件夹app下。 AppKernel类中覆盖下面的__construct函数:

<?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);
    }

}

找到了解决这个问题的类似方法(至少它对我有用)。

  1. 首先检查CLI php.ini所在的位置:

    php -i | grep "php.ini"

  2. 在我的情况下,我最终得到:配置文件(php.ini)路径=> /等

  3. 然后cd ..一路回来和cd进入/etc ,在我的情况下执行ls php.ini没有显示,只有一个php.ini.default

  4. 现在,复制名为php.ini的php.ini.default文件:

    sudo cp php.ini.default php.ini

  5. 要编辑,请更改文件的权限:

    sudo chmod ug+w php.ini

    sudo chgrp staff php.ini

  6. 打开目录并编辑php.ini文件:

    open .

    提示:如果由于某些权限问题而无法编辑php.ini,请复制'php.ini.default'并将其粘贴到桌面上并将其重命名为'php.ini'然后打开它并在步骤后编辑它7.然后在/ etc文件夹中移动(复制+粘贴)它。 问题将得到解决。

  7. 搜索[日期]并确保以下行的格式正确:

    date.timezone = "Europe/Amsterdam"

我希望这可以帮助你。

当前接受的破解答案在Symfony 2.3中已弃用,将被删除3.0。 它应该移动到构造函数:

public function __construct($environment, $debug) {
    date_default_timezone_set('Europe/Warsaw');
    parent::__construct($environment, $debug);
}

从PHP 5.5开始,CLI接口有一个单独的php.ini文件。 如果您从命令行使用symfony控制台,则使用此特定的php.ini。

在Ubuntu 13.10检查文件中:

/etc/php5/cli/php.ini

当我们在Redhat或Cent OS上使用PHP 5.1时会出现问题

RHEL / CentOS上的PHP 5.1不支持时区功能

sas的答案 ,PHP 5.4,Symfony 2.8,我不得不使用

ini_set('date.timezone','<whatever timezone string>');

而不是date_default_timezone_set 我还在自定义web/config.php的顶部添加了对ini_set的调用,以使该检查成功。

将此代码添加到您的AppKernel类:

public function init()
{
    date_default_timezone_set('Asia/Tehran');
    parent::init();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM