Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

sql

php

mysql

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!

like image 304
neeko Avatar asked Oct 27 '12 22:10

neeko


2 Answers

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.

like image 138
G-Nugget Avatar answered Sep 18 '22 00:09

G-Nugget


  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

like image 44
user4035 Avatar answered Sep 18 '22 00:09

user4035