ZendFramework 2:设置date_default_timezone

时间:2013-11-21 15:53:27

标签: php date zend-framework2

我是Zend Framework 2的新手,想知道是否有一种设置日期默认时区的全局方式。

我知道我应该能够添加代码:

date_default_timezone_set("UTC");

然而,我一直在寻找一个小时,但找不到解决这个问题的答案。

我也试过在php.ini中设置它,但我不确定这是否会抑制错误信息。

提前致谢。

5 个答案:

答案 0 :(得分:5)

我的优雅方式"使用onBootstrap覆盖配置文件中的php设置。在我的global.php中,我添加了我需要为应用程序设置的php设置:

return array(
   'php_settings' => array(
       'date.timezone' => 'UTC',
       'memory_limit' => '128M',
       'display_errors' =>'On'
   )
);

然后,onBootstrap:

    //Enable php settings from config file into the web app
    $services = $e->getApplication()->getServiceManager();
    $config = $services->get('config');        
    $phpSettings = $config['php_settings'];
    if ($phpSettings) {
        foreach ($phpSettings as $key => $value) {
            ini_set($key, $value);
        }
    }

答案 1 :(得分:3)

我只是在我的public/index.php文件中添加了PHP代码,其中Zend Framework被启动,或者在application/Bootstrap.php进程的早期。这确保它是全局的,并且在使用任何应用程序日期调用之前发生。

答案 2 :(得分:2)

最好的方法是在php.ini文件中设置它。这将适用于所有应用程序和直接可用的php wide(web和cli)。 php的时区应该是服务器范围的设置,所以把它放在服务器范围的配置(php.ini)中并不奇怪。

在php.ini中搜索date.timezone。您可以阅读更多相关信息in the manual。一个例子是date.timezone = UTC

答案 3 :(得分:1)

在onBootstrap函数的Module.php中为我添加date_default_timezone_set('UTC');工作。

答案 4 :(得分:0)

带有身份验证的Module.php示例(假设身份具有时区和语言环境的属性):

class Module {
    public function onBootstrap(MvcEvent $e) {
    //...your stuff

    //set the timezone
    $identity = $e->getApplication()
        ->getServiceManager()
        ->get('Zend\Authentication\AuthenticationService')
        ->getIdentity();

    if ($identity) {
        setlocale(LC_ALL, $identity->getLocale());
        date_default_timezone_set($identity->getTimezone());
    }
}