2011-12-18 127 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方法在更大的数据集上更快。

相关问题