日期功能显示两个日期之间的所有日期

时间:2013-02-13 10:18:46

标签: php date

是否有任何PHP函数可以显示两个日期之间的所有日期?

3 个答案:

答案 0 :(得分:28)

DatePeriod类。

示例:

$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

(P1D代表一天的时间,有关进一步的文档,请参阅DateInterval

答案 1 :(得分:3)

您也可以查看此功能

        $day = 86400; // Day in seconds  
        $format = 'Y-m-d'; // Output format (see PHP date funciton)  
        $sTime = strtotime($start_date); // Start as time  
        $eTime = strtotime($end_date); // End as time  
        $numDays = round(($eTime - $sTime) / $day) + 1;  
        $days = array();  

        for ($d = 0; $d < $numDays; $d++) {  
            $days[] = date($format, ($sTime + ($d * $day)));  
        }  

答案 2 :(得分:1)

    $start_date  = '2020/09/01';
    $end_date    = '2020-09-03';

    $new_date = new DateTime($end_date);
    $new_date->add(new DateInterval('P1D'));
    $end_date = $new_date->format('Y-m-d');

    $period = new DatePeriod(
    new DateTime($start_date),
    new DateInterval('P1D'),
    new DateTime($end_date)
   );

   print_r($period);
   foreach ($period as $key => $value) {
     echo $value->format('Y-m-d')  .'<br>';      
   }    

打印:
2020-09-01
2020-09-02
2020-09-03