datetime - How to Minus two dates in php -


this question has answer here:

i want minus 2 dates in php

for example:

$date1 = 08/16/2013; $date2 = 08/23/2013; $answer = date2 - date1; 

the $answer should 7, how that? thank much

start using datetime class date/time manipulation :

$date1 = new datetime('08/16/2013'); $date2 = new datetime('08/23/2013'); $diff = $date1->diff($date2); print_r($diff); // or $diff->days 

output :

dateinterval object (     [y] => 0     [m] => 0     [d] => 7     [h] => 0     [i] => 0     [s] => 0     [invert] => 0     [days] => 7 ) 

read more datetime:diff().


please note various strtotime() examples not correct in date/time difference calculation. simplest example difference between 2013-03-31 21:00 , 2013-03-30 21:00. naked eye exact 1 day difference, if subtract 2 dates, 82800 seconds 0.95833333333333 days. because of time change winter summer time. datetime handles leap years , time-zones properly.