我是Zend Framework 2的新手,只想知道是否有一种全局方式可以设置日期默认时区。
我知道我应该能够添加代码:
date_default_timezone_set("UTC");
然而,我已经找了大约一个小时,却找不到解决这个问题的答案。
我也尝试过在php.ini中设置它,但我不确定这是否会抑制错误消息。
提前谢谢。
我的"优雅方式"是使用onBootstrap从配置文件中覆盖php设置。在我的global.php中,我添加了需要为应用程序设置的php设置:
return array(
'php_settings' => array(
'date.timezone' => 'UTC',
'memory_limit' => '128M',
'display_errors' =>'On'
)
);
然后,在引导程序上:
//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);
}
}
我只需在Zend Framework启动的public/index.php
文件中添加PHP代码,或者在application/Bootstrap.php
进程的早期添加。这确保了它是全局的,并且在使用任何应用程序日期调用之前发生。
最好的方法是在php.ini文件中设置它。这将适用于php范围(包括web和cli),适用于所有应用程序并直接可用。php的时区应该是服务器范围的设置,所以把它放在服务器范围的配置(php.ini)中也不奇怪。
在php.ini中搜索date.timezone
。您可以在手册中阅读更多关于它的信息。例如date.timezone = UTC
。
在onBootstrap函数的Module.php中为我添加date_default_timezone_set('UTC');
Work。
带身份验证的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());
}
}