2011-02-02 20 views
37

如何使用codeigniter的activerecord查詢兩個日期之間的記錄來從數據庫檢索數據?codeigniter:在兩個日期之間發佈數據

感謝

+0

請進一步解釋,如果您已嘗試過任何操作,請發佈代碼。 – jondavidjohn 2011-02-02 14:42:43

+0

我不知道你在說什麼 – 2011-02-02 14:42:55

+0

你想在兩個日期之間從數據庫中提取數據嗎? – Jazzerus 2011-02-02 14:45:54

回答

89

這看起來像你需要:

$this->db->where('order_date >=', $first_date); 
$this->db->where('order_date <=', $second_date); 
return $this->db->get('orders'); 
9

試試這個:

$this->db->where('sell_date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"'); 

希望這將工作

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; 
    } 
0
$query = $this->db 
       ->get_where('orders',array('order_date <='=>$first_date,'order_date >='=>$second_date)) 
       ->result_array(); 
3

這爲我工作的偉大

$this->db->where('sell_date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"'); 
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) 。

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(); 
    } 
相關問題