Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP query, get rows from last 24 hours

Tags:

php

I am trying to make a report that shows all the loans made in the last day. The time is saved as a time stamp in the database onloan and is saved in the format yyyy-mm-dd 00:00:00. The code I have so far is shown below but i cannot get the time formatted correctly and -1 day.

$yday  = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
$query = "SELECT * 
          FROM onloan
          WHERE (time > $yday)";

Thanks in advance for any help!

like image 208
user1354895 Avatar asked Feb 20 '23 17:02

user1354895


2 Answers

You want to use strtotime().

Try $yday = date('Y-m-d h:i:s', strtotime("-1 day"))

like image 179
Jack Avatar answered Feb 23 '23 06:02

Jack


Try

SELECT * 
FROM onloan
WHERE DATE(Time) = DATE(CAST(NOW() - INTERVAL 1 DAY AS DATE))
like image 22
Baba Avatar answered Feb 23 '23 06:02

Baba