I have a subscription system in my website. When the users subscribe, their start date and time is added using the now()
function. I also need to calculate the date and time six months from now and store it in database. How can I do it?
Asked
Active
Viewed 1,502 times
-3
-
I tried that solution but didn't work but the below answer works fine @Glavić – Anbu369 Dec 01 '14 at 07:42
-
`DATE_ADD(CURDATE(), INTERVAL 6 MONTH)` returns `2015-06-01`, and `DATE_ADD(NOW(), INTERVAL 6 MONTH)` returns `2015-06-01 08:44:37`. So solution `DATE(DATE_ADD(NOW(), INTERVAL 6 MONTH))` doesn't make any sense. If you have `DATETIME` field use first solution, if you have `DATE` field use second solution. Btw, why do you store `Y-m-d` to `DATETIME` column? Can you show your table schema? – Glavić Dec 01 '14 at 07:47
4 Answers
2
DATE(DATE_ADD(NOW(), INTERVAL 6 MONTH))
can be used directly in your query and will do the job:
INSERT INTO table (datenow,dateplus6m) VALUES (NOW(), DATE(DATE_ADD(NOW(), INTERVAL 6 MONTH)));
Explanation from the docs:
DATE_ADD(date,INTERVAL expr unit)
These functions perform date arithmetic. The date argument specifies the starting date or datetime value. expr is an expression specifying the interval value to be added or subtracted from the starting date. expr is a string; it may start with a “-” for negative intervals. unit is a keyword indicating the units in which the expression should be interpreted.

baao
- 71,625
- 17
- 143
- 203
-
Hi, I tried your solution but When I use only NOW(), the values are stored in the database. But when I use the DATE_ADD function, none of the values are stored in the database. – Anbu369 Dec 01 '14 at 06:45
-
-
yes both are datetime columns. I have one username column and two datetime column. – Anbu369 Dec 01 '14 at 06:49
-
-
No. Still no entry in database. Here is the code I am using `mysqli_query($con,"INSERT INTO users (fb_id, fb_name, pro_start, pro_end) VALUES ('$fbid2', '$fbname2', NOW(), DATE_ADD (CURDATE(), INTERVAL 6 MONTH))");` – Anbu369 Dec 01 '14 at 06:57
-
1
Try this
$newDate = date('Y-m-d', strtotime("+6 months", time()));
echo $newDate;
OUTPUT
2015-06-01

Amit Malakar
- 618
- 1
- 5
- 10
0
You can use date function for adding months in php . Suppose $date contains current date,$date1 contains current date + 6 months
Here is the code
$date=date("Y-m-d");
echo $date;
$date1=date('Y-m-d',strtotime("6 months"));
echo $date1;
Here is the manual for other options
Hope this helps!

Sudhir kumar
- 549
- 2
- 8
- 31