Zabs
Zabs

Reputation: 14142

Show events from the last 7 days via MySQL

I am trying to just return all rows for an event that has closed within 7 days of the current date.

My end_date has a format such as 2014-06-25 (Y-m-d), what is the best way to select events between NOW and 7 days ago in the past.

I have the following.. but this isn't correct

SELECT * FROM end_date WHERE end_date <= NOW() AND end_date >= DATE_SUB(end_date, INTERVAL 7 DAY)

For instance... e.g If to day i'd want to say events between 2014-07-14 and 2014-07-07

Upvotes: 0

Views: 288

Answers (3)

SQLChao
SQLChao

Reputation: 7847

Try using DATE_ADD

SELECT * 
FROM end_date 
WHERE end_date <= NOW() AND end_date >= DATE_ADD(now(),INTERVAL -7 day)

Upvotes: 1

Azrael
Azrael

Reputation: 1094

I would suggest figuring out a way to get the current date - 7 days in the scripting format you are wanting to use it and then do a query like this:

SELECT * FROM end_date WHERE end_date >= $calculateddate; 

Upvotes: 0

Bla...
Bla...

Reputation: 7288

You can do something like below:

SELECT .... FROM .... WHERE DATEDIFF(NOW(), end_at) <= 7;

Upvotes: 0

Related Questions