代码点火器默认时区问题


Codeigniter Default Timezone Issue

简介:我正在开发CodeIgniter3.0.3版本的应用程序。它是一个多租户应用程序。单个代码库根据用户登录连接到多个数据库(与不同的数据库交互(。每个用户可能来自全球不同的时区。我正在尝试应用以下代码。

date_default_timezone_set("Africa/Accra");
$time =  Date('Y-m-d h:i:s');echo $time;
$expiry=$this->db->get_where('settings', array('type' => 'registration_date'))->row()->description;
$timestamp=strtotime($expiry);
echo Date('Y-m-d h:i:s',$timestamp);

此代码的第二行相应地正确显示时间,而最后一行显示服务器时间。

设置表的描述列为varchar类型,存储值如"2016-02-08 16:29:09"。

另外,如果我在最后一行之前添加date_default_timezone_set("非洲/阿克拉"(,它工作正常。

问题:我尝试在index.php,config.php,控制器构造方法,创建的帮助程序库中设置此属性,但似乎都没有工作。请问我该如何解决?

编辑:存储在数据库中的日期值是在应用此timezone_set之前。以前数据库中没有偏移量。

截至目前,我不知道每个用户的时区。我可以将其设置为 GMT 并相应地更新所有列。我是否需要根据时区设置更新所有现有日期列? 但是,如果任何用户更改其时区偏好,会发生什么?还有其他解决方法吗?

地方

date_default_timezone_set("Africa/Accra");

在索引中.php文件

Dhruv的回答在一定程度上有所帮助。但是显示以前存储的日期(在实现时区之前(是问题所在。然后跟进了几篇SO文章,得出了如下所示的解决方案。

date_default_timezone_set("Asia/Kolkata"); 
$registered=$this->db->get_where('settings', array('type' => 'registration_date'))->row()->description;
$dt_obj = new DateTime($registered);
$dt_obj->setTimezone(new DateTimeZone('Africa/Accra'));
echo $dt_obj->format('Y-m-d H:i:s');

即,首先根据服务器设置时区,检索数据并将其转换为显示

感谢这个问题中提到的gzipp

在 config.php 中,添加/修改以下块:

/*
|--------------------------------------------------------------------------
| Master Time Reference
|--------------------------------------------------------------------------
|
| Options are 'local' or any PHP supported timezone. This preference tells
| the system whether to use your server's local time as the master 'now'
| reference, or convert it to the configured one timezone. See the 'date
| helper' page of the user guide for information regarding date handling.
|
*/
$config['time_reference'] = 'local';

有关详细信息,请参阅本指南。

希望这对您有所帮助。