Wednesday, May 25

Mysql adding future dates in the query.

I had this application, where i had a column start_date where i had to insert the current timestamp and another column ie. end_date where i had to insert the current timestamp + 30 days.

We can do this in the mysql insert query using the mysql ADDTIME function

INSERT INTO table_name (end_date) VALUES (ADDDATE(CURRENT_TIMESTAMP, INTERVAL 30 DAY));

The start_date column type is set to CURRENT_TIMESTAMP so it will automatically be updated on INSERT query.

You can get more information about addtime() from here.

ADDTIME(expr1, expr2)

ADDTIME adds expr2 to expr1 and returns the result. expr1 is a time or datetime expression and expr2 is a time expression.

Be the first one to Comment!!!

Post a Comment