Difference of dates and then add days problem

Let’s say:
$termdate = 9/10/11
$addeddate = 8/11/11
$nowdate = 9/23/11

The result is calculated at 13 days diff and $newtermdate will be 09/23/11. That is fine.

However let’s say:
$termdate = 12/09/11
$addeddate = 8/11/11
$nowdate = 9/23/11

The result is still calculated at 13 days diff and $newtermdate will be 12/22/11. That is obviously wrong. Any ideas?


$termdate = $row['termdate'];
$addeddate = $row['date'];
$nowdate = date("m/d/y");

$diff = abs(strtotime($nowdate) - strtotime($addeddate));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

$newtermdate = strtotime('+'.$days.' day', strtotime($termdate));
$newtermdate = date('m/d/y', $newtermdate);

Why are you not using the PHP function date_diff?
PHP offers a great set of functions, already built, to manipulate dates and times.

I guess because I am using PHP 5.2 still

your question might be clearer to me if you gave an example of exact dates you have in your database, and exactly it is you want to derive from those 2 dates - a US type mm/dd/yyyy date? or a mysql ready date? And what is computation has to be done to get this date?

Dont describe them - paste the dates here. Give 2 or more examples if you want.

I’m sorry for not being clear. Those are actual examples of dates in my database.

I’m trying to find the difference in days between the $nowdate and $dateadded. The difference in days should be added to the $termdate.

$newdate = date(‘d/m/Y’,strtotime($termdate) + abs(strtotime($nowdate) - strtotime($addeddate)));

Dont bother doing the conversions. Slap everything into seconds, do the math, and then translate THAT.

I made it so difficult on myself! Thanks StarLion!

Even faster, since this is coming from the database use MySQL’s date functions.

From what the OP has shown I understand the OP is storing dates as mm/dd/yyyy and not yyyy-mm-dd, and that is one thing which is confusing me.

Never mind, PO has got a sensible reply and is happy.