Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the number of days between two dates

Tags:

php

datetime

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

like image 209
PHP Ferrari Avatar asked Jan 11 '10 08:01

PHP Ferrari


People also ask

How can I calculate days between two dates in SQL?

The DATEDIFF() function returns the difference between two dates.


2 Answers

$now = time(); // or your date as well $your_date = strtotime("2010-01-31"); $datediff = $now - $your_date;  echo round($datediff / (60 * 60 * 24)); 
like image 190
Adnan Avatar answered Oct 01 '22 01:10

Adnan


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

like image 25
Saksham Gupta Avatar answered Oct 01 '22 03:10

Saksham Gupta