views:

254

answers:

5

Hello,

In my table I have a field named eventdate in datetime format like 2010-05-11 00:00:00.

How do i make a query so that it adds one day to the eventdate eg if today is 2010-05-11, i want to show in where clause to return all records with tomorrow's date.

Update:

I tried this:

select * from fab_scheduler where custid = 1334666058 and DATE_ADD(eventdate, INTERVAL 1 DAY)

But unfortunately it returns the same record even if i add an interval greater than 1.

Result:

2010-05-12 00:00:00

But i only want to select records with tomorrow's date.

A: 

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");

Or, simplier:

date("Y-m-d H:i:s", time()+((60*60)*24));

Vafello
You didn't count on daylight savings time. mktime would be better.
Vili
+1  A: 

You can use the DATE_ADD() function:

... WHERE DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE

It can also be used in the SELECT statement:

SELECT DATE_ADD('2010-05-11', INTERVAL 1 DAY) AS Tomorrow;
+------------+
| Tomorrow   |
+------------+
| 2010-05-12 |
+------------+
1 row in set (0.00 sec)
Daniel Vassallo
please see my update...
@phpBOY: I think you missed the ` = CURRENT_DATE` part.
Daniel Vassallo
i tried this `select * from fab_scheduler where custid = 1334666058 and DATE(DATE_ADD(eventdate, INTERVAL 1 DAY)) = CURRENT_DATE` but now it returns empty result set :(
I would use CURDATE() personally
DavidYell
@phpBOY: You're right. You have to use `-1` to match with `CURRENT_DATE`. Try with the updated query.
Daniel Vassallo
@Daniel Vassallo: Could you please construct the query, iam getting empty record set. Thanks
@phpBOY: Did you try: `select * from fab_scheduler where custid = 1334666058 and DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE`
Daniel Vassallo
@David: Any reason why you prefer `CURDATE()`?... `CURDATE()` and `CURRENT_DATE` are synonyms according to [the docs](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_current-date).
Daniel Vassallo
Thanks it worked :)
That's just how I roll ;)
DavidYell
+1  A: 

SELECT DATE_ADD(eventdate, INTERVAL 1 DAY)

MySQL doc

Vili
A: 

Have a go with this, as this is how I would do it :)

SELECT * 
FROM fab_scheduler
WHERE custid = '123456'
AND eventdate = DATE(DATE_ADD(eventdate, INTERVAL 1 DAY))
DavidYell
@David: I don't think that can work. `eventdate` will never be equal to `eventdate + 1 day`. I think you intended to use `CURDATE()` in there.
Daniel Vassallo
Oh yes, so I did
DavidYell
A: 

It`s possible to use MySQL specific syntax sugar:

SELECT ... date_field + INTERVAL 1 DAY

Looks much more pretty instead of DATE_ADD function

Vadim