maxxxo
maxxxo

Reputation: 754

Check if difference between 2 dates is not more then 2 days

I have this check in my PHP script done in mySQL where I select entry which meet conditions and I need to enhance this one:

outDate > ".sqltext(date("Y-m-d H:i:s", strtotime($data["inDate"])))."

outDate has to be later then inDate, but it should be later maximum by 2 days. The query now:

$sql = " SELECT id  
         FROM inputs_outputs
         WHERE objectId=".sqltext($data["objectId"])." 
         AND nfcTagId= ".sqltext($data["nfcTagId"])." 
         AND nfcText = ".sqltext($data["nfcText"])."
         AND inDate < ".sqltext(date("Y-m-d H:i:s", strtotime($data["outDate"])))."
         AND inDate !='0000-00-00 00:00:00' 
         AND outDate = '0000-00-00 00:00:00' 
         ORDER BY inDate DESC LIMIT 1";

Upvotes: 0

Views: 2065

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32402

datediff returns the number of days between two dates, so you could use the following where clause to make sure that outDate is no more than 2 days greater than inDate

where outDate > inDate AND datediff(outDate,inDate) <= 2

Upvotes: 1

Related Questions