Page 1 of 1

Insert date after X days added to now() function in mysql

Posted: 01 Dec 2014, 06:42
by Marius
I have a subscription system in my website. When the users subscribe, their start date and time is added in mysql table using now() function.
I also need to insert the date and time six days from now and store it in database. How can I do it?

Insert date after X days added to now() function in mysql

Posted: 01 Dec 2014, 07:05
by Admin
The date_add() function is what you're looking for:

Code: Select all

DATE_ADD(date,INTERVAL expr unit)
Example

Code: Select all

INSERT INTO users
(username, registration_date, six_days_date)
VALUES ('someone', NOW(), DATE_ADD(NOW(), INTERVAL 6 DAY));
Or with PHP:

Code: Select all

$new_date = date('Y-m-d', strtotime("+6 days", time()));
$sql = "INSERT INTO users
(username, registration_date, six_days_date)
VALUES ('someone', NOW(), $new_date )";