简体   繁体   中英

Find results older than 2 days from date in SQL

In my database, I record the date the entry was made. I want to find entries which are 2 days earlier or older.

I've tried the following which doesn't work properly.

//Order is over 2 days old YES
$todaysDate = date("Y-m-d"); 
$todaysDatePlusDelay = date('Y-m-d', strtotime($todaysDate. ' + 2 days'));

With the SQL code

mysql_query("SELECT * FROM orders WHERE date >= $todaysDatePlusDelay") or die("error");

Can I just do all of this in SQL?

Date is shown in this type of format 2017-07-10

Why not just do this?

SELECT o.*
FROM orders o
WHERE o.date <= curdate() - interval 2 day;

Also, are you sure you don't really want - instead of + , to get data from the previous two days rather than the next two days.

(And use mysqli_ instead of mysql_.)

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