PHP get days between start-date and end-date

Francisc picture Francisc · Oct 13, 2010 · Viewed 11.9k times · Source

If I have two variables $startDate="YYYYmmdd" and $endDate="YYYYmmdd", how can I get the number of days between them please?

Thank you.

Answer

lonesomeday picture lonesomeday · Oct 13, 2010

If you are using PHP 5.3, you can use the new DateTime class:

$startDate = new DateTime("20101013");
$endDate = new DateTime("20101225");

$interval = $startDate->diff($endDate);

echo $interval->days . " until Christmas"; // echos 73 days until Christmas

If not, you will need to use strtotime:

$startDate = strtotime("20101013");
$endDate = strtotime("20101225");

$interval = $endDate - $startDate;
$days = floor($interval / (60 * 60 * 24));

echo $days . " until Christmas"; // echos 73 days until Christmas