PHP MySQL - 选择所有到期日期 = 今天的日期 + 7 天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13104639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP MySQL - Select all where expiry date = todays date + 7 days
提问by neeko
I am using PHPMyadmin and putting values in a database using PHP. I store the expiry date of products using a timestamp as follows, FOR EXAMPLE:
我正在使用 PHPMyadmin 并使用 PHP 将值放入数据库中。我使用时间戳存储产品的到期日期,如下所示,例如:
2012-11-04
I want to select all where the expiry date is equal to todays date plus 8 days (such as the one above)
我想选择到期日期等于今天日期加8天的所有地方(例如上面的那个)
I also want to select all where expiry date is equal to todays date + 2 weeks in a seperate page if any one could help me out would be very grateful!
我还想在单独的页面中选择到期日期等于今天日期 + 2 周的所有内容,如果有人可以帮助我,将不胜感激!
回答by G-Nugget
You can do that with a query like this:
你可以用这样的查询来做到这一点:
SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY)
You can use DATE_SUBfor dates in the past.
您可以DATE_SUB用于过去的日期。
回答by user4035
- Select all where the expiry date is equal to todays date plus 8 days
- 选择到期日期等于今天日期加上 8 天的所有内容
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 8 DAY)
- Select all where the expiry date is equal to todays date plus 2 weeks
- 选择到期日期等于今天日期加上 2 周的所有内容
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
These docs will be helpful for you:
这些文档将对您有所帮助:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

