PHP Select from MySQL where date field is 7 days in the future

后端 未结 6 1583
无人及你
无人及你 2021-01-28 12:30

I have an automatic checker that checks for domains that are going to expire within the next 7 days and it sends and email to the customer.

Im using this SQL Query:

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 13:25

    Assuming your expiry date is a correct datetime field

    expiry_date=DATE_ADD(NOW(), INTERVAL 7 DAY)
    

    more reliable than

    expiry_date=DATE(NOW() + INTERVAL 7 DAY)
    

    Actually thinking about it if you want exactly 7 days in the future you'd have to use a datediff too

    Something like this to return only requests where the expiry date is exactly 7 days from today.

    DATEDIFF(expiry_date,DATE(NOW() + INTERVAL 7 DAY))=7
    

    Though this isn't fool proof you're better off letting it check against all days between now and 7 days in the future and then if it is setting an "email sent" flag in your database so then you can confirm the email was actually sent rather than blindly trusting a script to have worked.

提交回复
热议问题