tags:

views:

43

answers:

5

Hi everyone,

I have a Calendar of Events table and I would like to select events with dates equal to or greater than today. When I use the following SELECT statement, it only retrieves events in the future (> NOW()):

<?php    
$db->query("SELECT * FROM events WHERE event_date >= NOW()");
?>

How would I go about selecting all events that are either today or in the future?

Thank you

+1  A: 

You are looking for CURDATE():

$db->query("SELECT * FROM events WHERE event_date >= CURDATE()");

Or:

$db->query("SELECT * FROM events WHERE event_date >= CURRENT_DATE()");
Sarfraz
Thanks! Worked like a charm
NightMICU
@NightMICU: That is great :)
Sarfraz
+1  A: 

If you care only about the date, not the time, use CURDATE() instead of NOW().

Of course you can alternatively use the synonym CURRENT_DATE (with or without parentheses after it), but the docs I pointed to give CURDATE as the first spelling;-).

Alex Martelli
A: 

Try

<?php
$db->query("SELECT * FROM events WHERE event_date >= NOW()");
?>

NOW() is the time at this time so it does not include the record of today but before now.

Hope this helps.

NawaMan
You're correct about why the records aren't appearing, but your SQL doesn't solve the situation.
OMG Ponies
A: 

Your issue is that now() is very accurate. So dates for today and before now because they started at the beginning of the day.

Use this:

select * from events where datediff(event_date, now()) >= 0;
Phil Wallach
+2  A: 

The reason that this query:

SELECT * FROM events WHERE event_date >= NOW()

...returns records from the future, is because NOW() includes the time as well as the date. And that time portion indicates the time at which the [function or triggering] statement began to execute. So that means the start of the day in a DATETIME data type looks like: 2010-06-24 00:00:00 (YYYY-MM-DD HH:MM:SS, to microsecond precision), which NOW() would show something like 2010-06-24 14:24:31...

Here are your options:

SELECT * FROM events WHERE event_date >= DATE(NOW())
SELECT * FROM events WHERE event_date >= DATE(CURRENT_TIMESTAMP)
SELECT * FROM events WHERE event_date >= CURRENT_DATE()
SELECT * FROM events WHERE event_date >= CURRDATE()
OMG Ponies
Very in-depth reply, thank you! Very useful for the future
NightMICU