Timothy Coetzee Timothy Coetzee - 1 year ago 70
PHP Question

php compare difference between two dates

Im trying to return the difference between 2 dates, i'm working according to the example found on stackoverflow

My Problem? Im getting completely the wrong results returned, the following code returns 30 years, 0 months, 9 days, when it should obviously be only 7 days or 1 week.

Code follows below:


$pickupDate = '2016-10-13';
$returnDate = 2016-10-20;

$diff = abs(strtotime($pickupDate) - strtotime($returnDate));
$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));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Any input appreciated

Answer Source

just put single quote in return date like $returnDate = '2016-10-20'; and you can use date_diff() function of php like,

$daysdiffernce = date_diff(date_create('2016-10-13'),date_create('2016-10-20'));
echo $daysdiffernce->format("%R%a days");

and this will give exactly +7days answer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download