2013-07-02 43 views
5

剛剛意識到爲什麼我的網站現在顯示所有日期時間變量爲-1小時......我第一次使用Codeigniter! (以前從未出現過這種問題)Codeigniter時區mysql設置

所以,我已經包含在我的主index.php文件下面的代碼

/* 
|--------------------------------------------------------------- 
| DEFAULT TIMEZONE 
|--------------------------------------------------------------- 
| 
| Set the default timezone for date/time functions to use if 
| none is set on the server. 
| 
*/ 

if(! ini_get('date.timezone')) 
{ 
    date_default_timezone_set('GMT'); 
} 

但是,它仍然顯示爲-1小時,所以我假設我需要設置某種對MySQL默認設置的...

我已經包括下面的代碼行中我的模型:

function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
     $this->db->query("SET time_zone='+0:00'"); 
    } 

仍然沒有區別...幫助!

我的代碼是:

<h3><?=date('D, jS F @ g:ia', strtotime($row->datetime))?></h3> 

的$按行>日期時間變量無非是從我的MySQL數據庫中的DATETIME列值更。鑑於回送的變量總是比我的數據庫中值少1小時...

我的模型代碼是:

function coming_up() 
{ 
    $this->db->query("SET time_zone='+0:00'"); 
$query = $this->db->query('SELECT * FROM events1 WHERE datetime >= NOW() ORDER BY datetime LIMIT 2'); 
return $query->result(); 
} 

回答

2

config file添加這些行,然後再檢查,這是爲我工作

$config['time_reference'] = 'gmt';# Default should be GMT 
date_default_timezone_set('UTC');# Add this line after creating timezone to GMT for reflecting 
+0

沒有改變我怕...... @Rohan庫馬爾 – user2505513

+0

檢查'插入query'它應該看起來像'$ this-> db-> query(「INSERT INTO TABLE SET time_zone ='+ 0:00'」);' –

+0

請參閱上文,我編輯了我的帖子以包含我的查詢代碼@Rohan Kumar仍然沒有區別! – user2505513

11

在config/autoload.php中,設置一個模型以加載每個頁面加載。然後調用$ this-> db-> query(「SET time_zone ='+ 0:00'」);在那個模型構造函數中。

配置/ autoload.php

$自動加載[ '模型'] =陣列( 'default_model'); //用於離, 「說 default_model」

在應用/模型,創建一個名稱爲「default_model.php」的新模型文件並添加以下代碼。

應用程序/模型/ default_model.php

類Default_model擴展CI_Model {

function __construct() 
{ 
    // Call the Model constructor 
    parent::__construct(); 
    $this->db->query("SET time_zone='+0:00'"); 
} 
    } 

在每個頁面加載,這個構造函數將被調用,mysql的時區將設置爲+ 0:00。

+0

如果你需要處理DST,你可以加載命名區'$ this-> db-> query(「SET time_zone ='America/Chicago'」);'** note **你需要有MySQL時區設置[mysql_tzinfo_to_sql](http://dev.mysql.com/doc/refman/5.1/en/mysql-tzinfo-to-sql.html) – zerodahero

+0

這真是太棒了,謝謝! –

+0

將'date_default_timezone_set('TIMEZONE_SET_FOR_DATABASE');'添加到'defined'('BASEPATH')行下方的application/config/config.php或'exit'('不允許直接腳本訪問')。 ');' –

-4

您可以設置它在你的項目文件夾中的index.php文件在頂部,後<?php

<?php 
date_default_timezone_set('Asia/Bangkok');