Getting data posted in between two dates
Asked Answered
E

12

48

How can I retrieve data from the database by querying records between two dates using CodeIgniter's activerecord?

Exenterate answered 2/2, 2011 at 14:35 Comment(4)
please explain further, and if you have tried anything, please post the code.Amenity
I have no idea what you're talking aboutMettlesome
Are you trying to pull data from your database between two dates?Preciousprecipice
yes I am trying to pull data from my database between two dates using codeigniter quriesExenterate
M
127

This looks like what you need:

$this->db->where('order_date >=', $first_date);
$this->db->where('order_date <=', $second_date);
return $this->db->get('orders');
Mettlesome answered 2/2, 2011 at 15:50 Comment(1)
@ThorpeObazee what if i have two number and want to that entered number is in between or not?Dereism
H
12

Try This:

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

Hope this will work

Hiding answered 4/1, 2012 at 11:39 Comment(0)
E
6

This worked great for me

$this->db->where('sell_date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
Ethanol answered 11/11, 2014 at 12:9 Comment(0)
S
6

if your date filed is timestamp into database then this is the easy way to get record

$this->db->where('DATE(RecordDate) >=', date('Y-m-d',strtotime($startDate)));
$this->db->where('DATE(RecordDate) <=', date('Y-m-d',strtotime($endDate)));
Schleiermacher answered 31/10, 2019 at 7:46 Comment(1)
Using DATE function in where clauses causes your indexing to fail. Then you have to bear performance issues.Offstage
T
5

This worked for me:

$this->db->where('RecordDate >=', '2018-08-17 00:00:00');
$this->db->where('RecordDate <=', '2018-10-04 05:32:56');
Trireme answered 10/10, 2018 at 13:33 Comment(0)
S
3

If you want to compare SQL dates, you can try this:

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

That worked for me (PHP and MySQL).

Sartor answered 12/2, 2016 at 10:0 Comment(0)
F
2

Just simply write BETWEEN '{$startDate}' AND '{$endDate}' in where condition as

->where("date BETWEEN '{$startDate}' AND '{$endDate}'")
Figment answered 24/5, 2019 at 3:55 Comment(0)
H
1

May this helpful to you.... With Join of Three Tables

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;
    }
Higinbotham answered 5/2, 2013 at 4:43 Comment(0)
G
1
$query = $this->db
              ->get_where('orders',array('order_date <='=>$first_date,'order_date >='=>$second_date))
              ->result_array();
Gertrudegertrudis answered 10/5, 2014 at 22:50 Comment(1)
I feel like the <= and >= might be the wrong way around, but I don't know what values they actually hold.Schleswigholstein
F
1

if you want to force using BETWEEN keyword on Codeigniter query helper. You can use where without escape false like this code. Works well on CI version 3.1.5. Hope its help someone.

if(!empty($tglmin) && !empty($tglmax)){
        $this->db->group_start();
        $this->db->where('DATE(create_date) BETWEEN "'.$tglmin.'" AND "'.$tglmax.'"', '',false);
        $this->db->group_end();
    }
Fineable answered 23/8, 2017 at 9:33 Comment(0)
H
0

If you are using DATETIME datatype then you can use:

$this->db->where('order_date BETWEEN "'. date('Y-m-d H:i:s', strtotime($start_date.' 00:00:00')). '" and "'. date('Y-m-d H:i:s', strtotime($end_date.' 23:59:59')).'"');
Healthy answered 23/7, 2022 at 14:17 Comment(0)
A
0
$start_date = $this->input->post('startDate');
$end_date  = $this->input->post('endDate');
    
$data['total_retailer'] = $this->company_model->where(
                    array(
                        'DATE(company_created_date) >=' => $start_date,
                        'DATE(company_created_date) <=' => $end_date,
                    )
                )
Anchoress answered 17/1, 2023 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.