Calculate Days Between Dates in PHP

You can use PHP’s date functions to calculate the number of days between two dates. Here is an example code:

$date1 = new DateTime('2021-10-01'); // 第一个日期
$date2 = new DateTime('2021-10-10'); // 第二个日期

$interval = $date1->diff($date2); // 计算日期间隔
$days = $interval->days; // 获取间隔天数

echo "间隔天数: " . $days;

In the code above, we first create two DateTime objects to represent the first date and the second date. Then, we use the diff() method to calculate the interval between the two dates, which returns a DateInterval object. Finally, by accessing the days property of the DateInterval object, we can get the number of days in the interval.

Running the above code will output: Interval days: 9.

bannerAds