241

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?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Qiao
  • 16,565
  • 29
  • 90
  • 117
  • 6
    DATE_ADD http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add – zod Oct 08 '10 at 03:36

5 Answers5

499

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
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
11

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);
SDC
  • 14,192
  • 2
  • 35
  • 48
Igor Qwerty
  • 111
  • 1
  • 2
6

Try doing: INSERT INTO table(data, date) VALUES ('$data', now() + interval 1 day)

5
INSERT INTO `table` ( `data` , `date` ) VALUES('".$data."',NOW()+INTERVAL 1 DAY);
mypetlion
  • 2,415
  • 5
  • 18
  • 22
user1239611
  • 51
  • 1
  • 2
3

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/subtract. It 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 add or subtract a time/date interval to a date value and then returns the result.

Examples

-- 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

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
ucMedia
  • 4,105
  • 4
  • 38
  • 46