How to Get Number of Days Between Two Dates in PHP?

We may face a requirement like finding the difference between two dates or to get the number of days between two days.Using the date_diff function you can count days between two dates in PHP.

Use the following script calculates the difference between two dates using PHP and shows the days count.

$datetime1 = new DateTime(‘2017-5-28’);
$datetime2 = new DateTime(‘2017-5-11’);

//difference between two dates
$diff = date_diff($datetime1,$datetime2);

//Total days
print ‘Days:’.$diff->days;
//Will print Days:17

Editorial Staff

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.