繁体   English   中英

Codeigniter默认时区问题

[英]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(“ Africa / Accra”),它的工作正常。

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

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

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

地点

date_default_timezone_set("Africa/Accra");

在index.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';

有关更多信息,请参阅本指南

希望对您有所帮助。

暂无
暂无

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

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