简体   繁体   中英

Adding (minutes / hours / days / months / years) to date when INSERT

How can I add (minutes / hours / days / months / years) to date in INSERT

INSERT INTO sample_table(value,expire) VALUES("Some text", NOW() + 4 MONTHS)

(SQL query or in PHP)

You need to use the DATE_ADD() function like so:

INSERT INTO sample_table (value, expire) 
VALUES ("Some text", DATE_ADD(NOW(), INTERVAL 4 MONTH));

It also works with other intervals such as the intervals that you requested in your question

You can find more about the DATE_ADD() function here:

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

You are missing the interval keyword:

INSERT INTO sample_table(value, expire)
    VALUES('Some text', NOW() + INTERVAL 4 MONTHS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM