2011-12-18 124 views
2

我有一個在MySQL表中的數據:如何在CodeIgniter中僅從日期時間獲取日期?

ID  dates     value 
1 2011-12-18 10:11:12  test1 
2 2011-12-18 12:11:12  test2 
3 2011-11-18 10:19:11  test3 

當我試圖讓模塊那不行值,我寫的代碼,如:

$query=$this->db->get_where('datas', array('dates' => date('Y-m-d'))); 

我需要從整天像數據只從2011年12月18日獲取數據。

感謝

回答

1

嘗試$this->db->like();

所以這將是

$query=$this->db->like('dates', array('dates' => date('Y-m-d'))); 

%將被自動添加

你也有DATAS爲重點,並根據日期應該是數據庫結構

0

你可以做到以下幾點:

在你的表,創建一個INT場名爲TS

當您將數據插入這個表,使用時間()方法插入時間戳到TS

$data = array(
    'dates' => '<your date value>' , 
    'value' => '<your value>' , 
    'ts' => time() 
); 

$this->db->insert('datas', $data); 

然後,查詢時:

使用mktime()方法創建一個時間戳範圍今天開始&結束。

$start = mktime() - date('h')*3600;  
$end = mktime(); 

$this->db->where('ts >=', $start); 
$this->db->where('ts <=', $end); 

請確保您已將ts列索引。這也會比上面的LIKE方法在更大的數據集上更快。

相關問題