简体   繁体   中英

PHP query, get rows from last 24 hours

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!

You want to use strtotime() .

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

Try

SELECT * 
FROM onloan
WHERE DATE(Time) = DATE(CAST(NOW() - INTERVAL 1 DAY AS DATE))

你可以尝试:

select * from onloan where date(time)=date(date_sub(now(),interval 1 day));

这是一种更简单的SQL方法。

$query = "SELECT * FROM onloan WHERE DATEDIFF(NOW(),time) <= 1";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM