Date Difference

function dateDifference($startDate, $endDate){ $startDate = strtotime($startDate); //echo $endDate = strtotime($endDate); if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate) return false; $years = date('Y', $endDate) - date('Y', $startDate); $endMonth = date('m', $endDate); $startMonth = date('m', $startDate); // Calculate months $months = $endMonth - $startMonth; if ($months <= 0) { $months += 12; $years--; } if ($years < 0) return false; // Calculate the days $measure = ($months == 1) ? 'month' : 'months'; $days = $endDate - strtotime('+' . $months . ' ' . $measure, $startDate); $days = date('z', $days); return array($years, $months, $days); }

1 Response

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.