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?
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?
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
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);
Try doing: INSERT INTO table(data, date) VALUES ('$data', now() + interval 1 day)
INSERT INTO `table` ( `data` , `date` ) VALUES('".$data."',NOW()+INTERVAL 1 DAY);
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 ]
MICROSECOND | SECOND | MINUTE |
HOUR | DAY | WEEK |
MONTH | QUARTER | YEAR |
See the full list here.
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 add or subtract a time/date interval to a date value and then returns the result.
-- Results were the 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
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