Nick
Nick

Reputation: 17

how to get the next three events from mysql database starting with closest date to today

I'm connecting to my MySQL database using the PDO method. i have events stored with event_date as a date type. how do i select the 3 next events after today's date? do i need to switch event_date from date to a timestamp? I keep my dates stored in this format yyyy-mm-dd and they have to be like that for other parts of code to work.

i tried this but it didnt really work:

SELECT *
FROM events
WHERE event_date > NOW()
ORDER BY event_date ASC
LIMIT 3

pretty new to pdo and im not good with date and time functions in general :/ thanks!

Upvotes: 0

Views: 65

Answers (1)

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

SELECT *,DATEDIFF(curdate(),event_date) as closetevent 
   from events 
   Order by closetevent ASC 
   LIMIT 3

Upvotes: 1

Related Questions