Finding the number of days between two dates


Question

How to find number of days between two dates using PHP?

1
579
6/24/2019 8:26:06 PM

Accepted Answer

$now = time(); // or your date as well
$your_date = strtotime("2010-01-31");
$datediff = $now - $your_date;

echo round($datediff / (60 * 60 * 24));
857
5/15/2019 6:06:20 AM

If you're using PHP 5.3 >, this is by far the most accurate way of calculating the difference:

$earlier = new DateTime("2010-07-06");
$later = new DateTime("2010-07-09");

$diff = $later->diff($earlier)->format("%a");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon