如何使用codeigniter的activerecord通过查询两个日期之间的记录来检索数据库中的数据?
感谢
答案 0 :(得分:106)
这看起来像你需要的:
$this->db->where('order_date >=', $first_date);
$this->db->where('order_date <=', $second_date);
return $this->db->get('orders');
答案 1 :(得分:9)
试试这个:
$this->db->where('sell_date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
希望这会起作用
答案 2 :(得分:4)
这对我很有用
$this->db->where('sell_date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
答案 3 :(得分:2)
这对我有用:
$this->db->where('RecordDate >=', '2018-08-17 00:00:00');
$this->db->where('RecordDate <=', '2018-10-04 05:32:56');
答案 4 :(得分:1)
如果您提交的日期是时间戳,那么这是获取记录的简便方法
$this->db->where('DATE(RecordDate) >=', date('Y-m-d',strtotime($startDate)));
$this->db->where('DATE(RecordDate) <=', date('Y-m-d',strtotime($endDate)));
答案 5 :(得分:0)
这对你有帮助.... 加入三张桌子
public function get_details_beetween_dates()
{
$from = $this->input->post('fromdate');
$to = $this->input->post('todate');
$this->db->select('users.first_name, users.last_name, users.email, groups.name as designation, dailyinfo.amount as Total_Fine, dailyinfo.date as Date_of_Fine, dailyinfo.desc as Description')
->from('users')
->where('dailyinfo.date >= ',$from)
->where('dailyinfo.date <= ',$to)
->join('users_groups','users.id = users_groups.user_id')
->join('dailyinfo','users.id = dailyinfo.userid')
->join('groups','groups.id = users_groups.group_id');
/*
$this->db->select('date, amount, desc')
->from('dailyinfo')
->where('dailyinfo.date >= ',$from)
->where('dailyinfo.date <= ',$to);
*/
$q = $this->db->get();
$array['userDetails'] = $q->result();
return $array;
}
答案 6 :(得分:0)
$query = $this->db
->get_where('orders',array('order_date <='=>$first_date,'order_date >='=>$second_date))
->result_array();
答案 7 :(得分:0)
如果您想比较SQL日期,可以试试这个:
$this->db->select();
$this->db->from('table_name');
$this->db->where(' date_columnname >= date("'.$from.'")');
$this->db->where( 'date_columnname <= date("'.$to.'")');
这对我有用(PHP和MySQL)。
答案 8 :(得分:0)
如果要在Codeigniter查询助手上强制使用BETWEEN关键字。你可以使用没有转义的地方,就像这段代码一样。适用于CI 3.1.5版。希望能帮助别人。
if(!empty($tglmin) && !empty($tglmax)){
$this->db->group_start();
$this->db->where('DATE(create_date) BETWEEN "'.$tglmin.'" AND "'.$tglmax.'"', '',false);
$this->db->group_end();
}
答案 9 :(得分:0)
只需在条件为“ {$ startDate}”和“ {$ endDate}”之间写
->where("date BETWEEN '{$startDate}' AND '{$endDate}'")