Query to return only the records of the last 24h (Codeigniter)

1

Hello I need to do a query that returns me the values of only the last 24h

I have the following query:

            $date = date("Y-m-d H:i:s");
            $this->db->select('*');
            $this->db->from('inscricoes'); 
            $this->db->where('data <', $date);
            $this->db->where('data >', $date-1);
            $query = $this->db->get();

but is returning all values

    
asked by anonymous 03.08.2016 / 13:24

1 answer

3

If you want the last 24 hours counting from the current time and day, you can use the strtotime function and subtract 24 hours from the current date.

You can add BETWEEN in your query:

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

$minDate = date("Y-m-d H:i:s", strototime('-24 hours');

$this->db->where(
     "data BETWEEN $minDate AND $maxDate", NULL, FALSE
);

Now if you want the activities just inside the current day, you can do so:

 $maxDate = date('Y-m-d H:i', strtotime('now 23:59:59'))

 $minDate = date('Y-m-d H:i', strtotime('now 00:00:00'));

Reference was taken from the response from Stackoverflow English and adapted by me.

    
03.08.2016 / 13:31