How to count days between two dates in PHP?

You can use the DateTime class in PHP to calculate the number of days between two dates. Here is an example:


$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2022-01-15');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');

Watch a course Learn object oriented PHP

This will output +14 days In this example, $date1 is set to January 1st, 2022 and $date2 is set to January 15th, 2022. The diff() method is used to calculate the difference between the two dates and the format() method is used to output the result in the desired format. The %R is used to determine if the date is in the past or in the future, and %a is used to output the difference in days.