Add days to current date from MySQL with PHP
Asked Answered
I

4

6

I have a fixed date from MySql

startDate = 07/03/2011

I wanted to add 60 days on top this date to have an endDate.

$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;

From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.

Help? Thanks in advance!

Irishirishism answered 3/7, 2011 at 18:6 Comment(0)
V
9

In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:

SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;

-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;

Relevant documentation here...

Veroniqueverras answered 3/7, 2011 at 18:11 Comment(1)
In fact you don't need the DATE_ADD(), it's just about the INTERVAL. Try: SELECT startDate, startDate + INTERVAL 2 MONTH AS endDate FROM table;Kale
B
5

You could reformat the results of strtotime()

$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));

Demo: http://codepad.org/9rWnoeQb

Behm answered 3/7, 2011 at 18:11 Comment(0)
D
2
$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;
Dyson answered 3/7, 2011 at 18:13 Comment(0)
P
1

86400 seconds in a day, times number of days.. and add it to current time.

$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);  
Pahoehoe answered 3/7, 2011 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.