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

Posted

技术标签:

【中文标题】Symfony2 和 date_default_timezone_get() - 依赖系统的时区设置是不安全的【英文标题】:Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings 【发布时间】:2014-01-11 15:26:54 【问题描述】:

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

这很奇怪。

有什么想法吗?谢谢。

【问题讨论】:

你能确认一下你改过的php.ini的完整路径吗 如果您在 symfony 控制台中收到此警告,那么您还需要在 cli/php.ini 中进行更改 【参考方案1】:

跟进sas's answer,PHP 5.4,Symfony 2.8,我不得不使用

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

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

【讨论】:

【参考方案2】:

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

    首先检查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.ini

    sudo chgrp staff php.ini

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

    open .

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

    搜索 [Date] 并确保以下行格式正确:

    date.timezone = "Europe/Amsterdam"

我希望这可以帮助你。

【讨论】:

你刚刚让我头疼不已。谢谢你,朋友! 从技术上讲,这应该是正确的答案,因为它实际上可以长期解决问题。【参考方案3】:

也许您正试图在 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);
    


【讨论】:

谢谢,但已检查:已加载配置文件 => /usr/local/php5.5.7/lib/php.ini cli应该还有其他配置,你也检查了吗? 嗯,如何找到第二个? 重启你的 apache2 也会重启 php 我遇到了同样的问题,您的另一个解决方案也适用于我【参考方案4】:

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

public function init()

    date_default_timezone_set('Asia/Tehran');
    parent::init();

【讨论】:

init 自 2.3 版起已弃用,将在 3.0 版中删除。将您的逻辑移动到构造函数中,而不是像@Krzysztof Bociurko 或更新后的接受答案。【参考方案5】:

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

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

【讨论】:

感谢您的回答。它解决了我在版本 3 中的问题【参考方案6】:

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

RHEL/CentOS 上的 PHP 5.1 不支持时区函数

【讨论】:

【参考方案7】:

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

在 Ubuntu 13.10 检查文件:

/etc/php5/cli/php.ini

【讨论】:

以上是关于Symfony2 和 date_default_timezone_get() - 依赖系统的时区设置是不安全的的主要内容,如果未能解决你的问题,请参考以下文章

Symfony2 和 Doctrine:一对多关系

CSRF 和 RESTful API (symfony2, php)

PHPUnit、接口和命名空间 (Symfony2)

为Symfony2和Redis正名,基于PHP的10亿请求/周网站打造

Symfony2 翻译 URL 和路由

Symfony2 从超类确定和访问子类