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

后端 未结 6 1565
无人及你
无人及你 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:01

    This should give the result as you need:

    DATE_SUB(CURDATE(),INTERVAL 7 DAY) = expiry_date;
    

    You can refer to the link below for more details:

    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

    0 讨论(0)
  • 2021-01-28 13:03

    If you need to advise the rows missed during the checker stops running, meaby you should use a extra tag to indicate if the domain have been advise or not. After that, you must select all the rows with date lower than now + 7 days and tag as "not advised". After sending the email tag it as "advised". And when they re-new the domain tag back as "not advise".

    0 讨论(0)
  • 2021-01-28 13:05
    $date=date('Y-m-d');
    $date=date('Y-m-d', strtotime($date . ' + 7 day'));
    
    
    //Now, use this query.
    
    SELECT * from domain_names where status = '' or status = 'valid' and expiry_date = '{$date}';
    
    0 讨论(0)
  • 2021-01-28 13:20

    You have to use parentheses with your and/or conditions

    SELECT * from domain_names 
    where (status = '' or status = 'valid')
    and expiry_date = curdate() + INTERVAL 7 DAY
    

    or use in in your case

    SELECT * from domain_names 
    where status in ('', 'valid')
    and expiry_date = curdate() + INTERVAL 7 DAY
    
    0 讨论(0)
  • 2021-01-28 13:23

    You've probably defined expiry_date as a datetime value, which means your comparisons are incorrect. e.g. you need to use

    SELECT ... WHERE date(expiry_date) = date(now() + interval 7 day)
    

    instead (note the wrapping of the +7 day in a date() operation.

    e.g.

    Given a table with a date and a datetime field:

    +------------+---------------------+
    | d          | dt                  |
    +------------+---------------------+
    | 2013-06-28 | 2013-06-28 08:23:03 |
    +------------+---------------------+
    

    Notice how the comparison comes out:

    mysql> select d=now(), d=date(now()), dt=now(), dt=date(now()), now() from x;
    +---------+---------------+----------+----------------+---------------------+
    | d=now() | d=date(now()) | dt=now() | dt=date(now()) | now()               |
    +---------+---------------+----------+----------------+---------------------+
    |       0 |             1 |        0 |              0 | 2013-06-28 08:26:20 |
    +---------+---------------+----------+----------------+---------------------+
    1 row in set (0.00 sec)
    

    date v.s. datetime = false
    date v.s date = true
    datetime v.s. datetime = false (hh:mm:ss doesn't match, so not equal)
    datetime v.s. date = false (date is expanded out to yyyy-mm-hh 00:00:00 and the hh:mm:ss don't match

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题