php - difference between two dates

php DateTime::diff manual (PHP 5.3+)

$date1 = new DateTime('2007-03-24');
$date2 = new DateTime('2009-06-26');
$interval = $date1->diff($date2);
echo 'difference: ' . $interval->y . ' years, ' . $interval->m.' months, '.$interval->d.' days ';

Variant for PHP version to 5.3

$date1 = '2007-03-24';
$date2 = '2009-06-26';
$diff = abs(strtotime($date2) - strtotime($date1));
$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('difference: %d years, %d months, %d days', $years, $months, $days);

Stackoverflow discussion.

Leave a Comment