christianleroy
christianleroy

Reputation: 1104

How to select a record with upcoming date and time?

I have this table called events. This table contains event_date and event_time in separate columns. I'm currently working on a page where it will show all the upcoming events. How do I query records from the events table where the date and time are still upcoming? I mean, how do I calculate or write the query, specifically the where clause, using MySQL? I'm using PHP and CodeIgniter.

Thanks to everyone who will answer.

EDIT: I used Gordon Linoff's suggestion:

where event_date >= now() or
      (event_date = date(now()) and event_time > time(now()))

But I modified it to

where event_date>date(now()) or
      (event_date=date(now()) and event_time>time(now()))

Because Gordon's didn't quite return the result I was expecting (Events which are dated on the current day but the time has not yet come were not appearing as upcoming events). Anyway, thanks Gordon and to other people who answered. I am not yet an expert in MySQL date queries, so you guys did me a huge favor.

Upvotes: 0

Views: 272

Answers (2)

Arun Kumar M
Arun Kumar M

Reputation: 848

SELECT * FROM events where event_date > DATE(NOW()) AND event_time > TIME(NOW());

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269443

You would use:

where event_date >= now() or
      (event_date = date(now()) and event_time > time(now()))

Upvotes: 2

Related Questions