MySQL's now() +1 day

Viewed 363459

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?

5 Answers

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
Related