Mysql select dates in the 30-day range

It should be simple, but I messed with him and didn’t get anything I wanted. I have the following code:

SELECT id,title,start_date 
  FROM events 
 WHERE start_date > DATE_SUB(NOW(), INTERVAL 1 MONTH) 
  AND city = '$cityName' 
ORDER BY start_date DESC

Now it selects events with dates in this month, but the definition of that month shown in the request is different from what I need. I need this to show me events for 30 days, and not only this month, that is August. If I insert an event in August, it shows the result. If I insert September, this is even though it is less than 30 days.

+5
source share
4 answers

You must change 1 MONTHto 30 DAY:

WHERE start_date > NOW() - INTERVAL 30 DAY

To limit it to 30 days in any direction:

WHERE start_date > NOW() - INTERVAL 30 DAY
AND start_date < NOW() + INTERVAL 30 DAY
+39
source

How about this:

...WHERE DATE(start_date) BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) and DATE_SUB(NOW(),INTERVAL 1 DAY) AND city...
+2
source

AND TIMESTAMPDIFF(DAY,YOURDATE,now()) < 30

30-

+1

I hope this also helps

SELECT id,title,start_date 
  FROM events 
 WHERE  city = "$cityName" AND 
TIMESTAMPDIFF(DAY,start_date,now()) < 30   
ORDER BY start_date DESC
+1
source

All Articles