neeko
neeko

Reputation: 2000

PHP MySQL - Select all where expiry date = todays date + 7 days

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:

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)

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!

Upvotes: 9

Views: 37966

Answers (2)

G-Nugget
G-Nugget

Reputation: 8846

You can do that with a query like this:

SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY)

You can use DATE_SUB for dates in the past.

Upvotes: 17

user4035
user4035

Reputation: 23759

  1. Select all where the expiry date is equal to todays date plus 8 days
SELECT
    *
FROM
    products
WHERE
    products.expiry_date >= DATE(now())
AND
    products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 8 DAY)
  1. Select all where the expiry date is equal to todays date plus 2 weeks
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

Upvotes: 13

Related Questions