MySQL's now() +1 day
SqlMysqlDatetimeSql Problem Overview
I'm using now()
in MySQL query.
INSERT INTO table SET data = '$data', date = now()
But I want to add 1 day to this date (so that date
should contain tomorrow).
Is it possible?
Sql Solutions
Solution 1 - Sql
You can use:
NOW() + INTERVAL 1 DAY
If you are only interested in the date, not the date and time then you can use CURDATE instead of NOW:
CURDATE() + INTERVAL 1 DAY
Solution 2 - Sql
better use quoted `data`
and `date`
. AFAIR these may be reserved words
my version is:
INSERT INTO `table` ( `data` , `date` ) VALUES('".$date."',NOW()+INTERVAL 1 DAY);
Solution 3 - Sql
Try doing: INSERT INTO table(data, date) VALUES ('$data', now() + interval 1 day)
Solution 4 - Sql
INSERT INTO `table` ( `data` , `date` ) VALUES('".$data."',NOW()+INTERVAL 1 DAY);
Solution 5 - Sql
Do you want to add time to a date value? MySQL interval values are used for date and time calculations. There are multiple ways to create an interval value.
One way is to use the following expression in your queries:
date [ + or - ] INTERVAL value [ UNIT ]
- UNIT is the type of interval to add/subtrac. Can be one of the following values
MICROSECOND | SECOND | MINUTE |
HOUR | DAY | WEEK |
MONTH | QUARTER | YEAR |
See the full list here.
- Notice that the INTERVAL and UNIT are case-insensitive.
Date arithmetic is less straightforward than time arithmetic due to the varying length of months and years, so MySQL provides special functions. Alternatively, you could use the DATE_ADD() or DATE_SUB() functions that adds or subtracts a time/date interval to a date value and then returns the result.
Examples
-- Results were same for all , '2018-05-02'
DATE_ADD('2018-05-01',INTERVAL 1 DAY);
'2018-05-01' + INTERVAL 1 DAY
'2018-05-01' + INTERVAL 24 HOUR
More examples
SELECT DATE_ADD('2100-12-31 23:59:59',INTERVAL '1:1' MINUTE_SECOND);-- '2101-01-01 00:01:00'
SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);-- '1997-12-02'
INSERT INTO `table` ( `title` , `date` )
VALUES('tomorrow never dies',NOW()+INTERVAL 1 DAY); --insert tomorrow date
SELECT DATE_ADD( NOW(), INTERVAL 1 DAY);--tomorrow date
SELECT NOW() + INTERVAL 1 DAY;--tomorrow date