Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add days to current date from MySQL with PHP

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!

like image 700
Arnold Porche Villaluz Avatar asked Jul 03 '11 18:07

Arnold Porche Villaluz


1 Answers

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...

like image 75
Michael Berkowski Avatar answered Sep 21 '22 01:09

Michael Berkowski