php通过给定的两个日期,生成期间所有日期

<?php  
  
function getDateRange($startDate, $endDate) {  
    $begin = new DateTime($startDate);  
    $end = new DateTime($endDate);  
  
    $returnArray = array();  
    $currentDate = clone $begin;  
  
    $interval = new DateInterval('P1D'); // 1天  
    $period = new DatePeriod($currentDate, $interval, $end->modify('+1 day'));  
  
    foreach ($period as $date) {  
        $returnArray[] = $date->format("Y-m-d");  
    }  
  
    return $returnArray;  
}  
  
// 示例  
$startDate = '2023-07-01';  
$endDate = '2023-07-10';  
  
$dates = getDateRange($startDate, $endDate);  
  
// 输出结果  
foreach ($dates as $date) {  
    echo $date . "\n";  
}  
  
?>

Was this helpful?

0 / 0

发表回复 0