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

Asked

Viewed 73 times

1

Hello I needed to do a query that returns 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

  • Two Where? is conflict, make 1 variable to join text Where blablabla and then $this->db->where(variavel);

1 answer

3


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

You can add BETWEEN to 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 only within 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 reply of Stackoverflow English and adapted by me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.