15
2024
05

php 获取两个时间之间所有的天

function getDatesBetweenTwoDays($start, $end) {
$start = new DateTime($start);
$end = new DateTime($end);
$end->add(new DateInterval('P1D')); // 添加一天,以包含结束日期
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$dates = array();
foreach ($period as $date) {
$dates[] = $date->format('Y-m-d');
}
return $dates;
}
// 使用示例
$startDate = '2023-01-01';
$endDate = '2023-01-05';
$dates = getDatesBetweenTwoDays($startDate, $endDate);
print_r($dates);

Array
(
[0] => 2023-01-01
[1] => 2023-01-02
[2] => 2023-01-03
[3] => 2023-01-04
[4] => 2023-01-05
)


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。