Codeigniter模型从-7天获取行


Codeigniter Model fetch rows from -7 days

如何获取从当前日期-1天的所有行?

public function getNumTasksDueTomorrow() 
{
    $this->db->select('*');
    $this->db->from('tasks'); 
    $this->db->where("user_id",$this->session->userdata('user_id'));  
    $this->db->order_by("tasks_id", "desc");    
    $this->db->where("tasks_duedate", ' -1 day from current day');
    $query_result=$this->db->get();
    $result=$query_result->row();
    return $result;
}      

首先获取昨天的mysql日期,如:

$date = date('Y-m-d H:i:s', strtotime('yesterday'));

然后将日期查询改为:

$this->db->where("tasks_duedate <", $date);

这将为您提供所有截止日期比昨天日期早的任务。但是,我认为您是指过去的任何日期,所以只需将$date更改为now。

 $date = date('Y-m-d H:i:s');

当然我假设你的日期记录是一个mysql时间戳,你可能已经使用了一个日期,只是删除时间格式。

希望有帮助,

保罗。