2011-02-02 25 views
37

codeigniterのactiverecordを使用して2つの日付間のレコードを照会することで、データベースからデータを取得するにはどうすればよいですか?codeigniter:2つの日付の間にデータを取得する

おかげ

これはあなたが必要なもののように見える
+0

何か試してみたら、コードを投稿してください。 – jondavidjohn

+0

あなたは何を話しているのか分かりません –

+0

2つの日付の間にデータベースからデータを取得しようとしていますか? – Jazzerus

答えて

89

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

datetimeフィールドでこのコードを使用できますか? –

+0

@ThorpeObazeeもし私が2つの番号を持っていて、その番号を間に入れたいのであれば? –

9

はこれを試してみてください。これは動作します

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

希望

0

月あなたに、この便利.... 3つのテーブルの結合あり

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

このを使用すると、SQLの日付を比較したい場合は

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

作品は完璧に感謝@マドフ – amyogiji

0

、あなたはこれを試すことができます私のために素晴らしい仕事:(PHPとMySQLの)私のために働い

$this->db->select(); 
$this->db->from('table_name'); 
$this->db->where(' date_columnname >= date("'.$from.'")'); 
$this->db->where('date_columnname <= date("'.$to.'")'); 

0

CodeigniterクエリヘルパーでBETWEENキーワードを強制的に使用する場合は、このコードのように、エスケープのないfalseをどこでも使用できます。 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(); 
    } 
関連する問題