Here is a sample code to count all dates between a given start and end date:
<?php
function getDatesBetween($start_date, $end_date) {
// 初始化结果数组
$dates = array();
// 将开始日期转换为时间戳
$current_date = strtotime($start_date);
$end_date = strtotime($end_date);
// 循环直到当前日期大于结束日期
while ($current_date <= $end_date) {
// 将当前日期添加到结果数组中
$dates[] = date('Y-m-d', $current_date);
// 增加一天
$current_date = strtotime('+1 day', $current_date);
}
return $dates;
}
// 示例用法
$start_date = '2022-01-01';
$end_date = '2022-01-10';
$result = getDatesBetween($start_date, $end_date);
// 输出结果
foreach ($result as $date) {
echo $date . "\n";
}
The above code defines a getDatesBetween
function called that takes a start date and an end date as parameters and returns an array of all dates between those two dates.
Using the start date of the example and 2022-01-01
the end date of 2022-01-10
, calling getDatesBetween
the function will return an array containing all dates from the start date to the end date.
Then use foreach
a loop to iterate through the resulting array and output each date row by row.