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

后端 未结 6 1567
无人及你
无人及你 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条回答
  •  梦毁少年i
    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

提交回复
热议问题