Satch3000
Satch3000

Reputation: 49384

Select all where date is present of future only

I know this code works:

$eventdate = new DateTime($event['date']);
$datenow = new DateTime();

if($eventdate->format('Y-m-d') < $datenow->format('Y-m-d')) ....

I need to do something similar in an SQL query.

Something like:

SELECT * FROM MyTable WHERE `date` = `$eventdate` or Future Date

How can I do this?

Upvotes: 0

Views: 115

Answers (3)

Andrea Rastelli
Andrea Rastelli

Reputation: 617

Something like:

SELECT
  *
FROM
  `table`
WHERE
  `date_field` >= NOW();

This should work as in this example: http://sqlfiddle.com/#!2/225a3/2

Upvotes: 0

Glavić
Glavić

Reputation: 43552

$sql = "
    SELECT * 
    FROM MyTable 
    WHERE 
        `date` = '{$eventdate->format('Y-m-d')}' OR 
        `date` >= CURDATE()
";

or

$sql = "
    SELECT * 
    FROM MyTable 
    WHERE 
        `date` = '{$eventdate->format('Y-m-d')}' OR 
        `date` >= '{$datenow->format('Y-m-d')}'
";

Upvotes: 1

Mina
Mina

Reputation: 1516

SELECT * FROM MyTable WHERE `date` >= `$eventdate`

Upvotes: 0

Related Questions