Dustin
Dustin

Reputation: 147

How do I SELECT only rows that are greater than today's date and time?

Forgive me if this is a simple question, but I am trying to echo out rows that are only in the future, ie: greater than today's date and time. This is my code so far:

$date = date('Y-m-d');
$time = date("G:i:s", time());

$eventsquery = "SELECT * FROM events as A WHERE tech='$userecho' AND date >= '$date' ORDER BY date ASC, time ASC";
$eventsresult = mysql_query($eventsquery, $eventsdbhandle);

This works great for giving me rows that are either for today or in the future, This problem is, this will give me entries that happened before the current time on the current day. So I tried adding this:

$eventsquery = "SELECT * FROM events as A WHERE tech='$userecho' AND date >= '$date' AND time >= '$time' ORDER BY date ASC, time ASC";

But I get no rows at all. I know this is likely an easy fix, but I have found nothing on this site that fixes it. Any help would be great.

Time in the table is stored in this format: hh:mm:ss ie: 14:23:12

Upvotes: 1

Views: 1855

Answers (2)

Ela Buwa
Ela Buwa

Reputation: 1704

If you have 1 column of datetime value you can say

"SELECT * FROM table_name WHERE entry_time > NOW()"

However, it gets complicated if you have two different columns for date and time each You will have to use

"SELECT * FROM table_name WHERE entry_date > CURDATE() OR (entry_date = CURDATE() AND entry_time > CURTIME()"

Upvotes: 3

peterm
peterm

Reputation: 92785

One way to do it

SELECT * 
  FROM events
 WHERE tech = ?
   AND ADDTIME(date, time) > NOW()
 ORDER BY ADDTIME(date, time)

Here is SQLFiddle demo

Upvotes: 2

Related Questions