簡體   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