How to find number of days between two dates using PHP?
The DATEDIFF() function returns the difference between two dates.
$now = time(); // or your date as well $your_date = strtotime("2010-01-31"); $datediff = $now - $your_date; echo round($datediff / (60 * 60 * 24));
If you're using PHP 5.3 >
, this is by far the most accurate way of calculating the absolute difference:
$earlier = new DateTime("2010-07-06"); $later = new DateTime("2010-07-09"); $abs_diff = $later->diff($earlier)->format("%a"); //3
If you need a relative (signed) number of days, use this instead:
$earlier = new DateTime("2010-07-06"); $later = new DateTime("2010-07-09"); $pos_diff = $earlier->diff($later)->format("%r%a"); //3 $neg_diff = $later->diff($earlier)->format("%r%a"); //-3
More on php's DateInterval
format can be found here: https://www.php.net/manual/en/dateinterval.format.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With