我以日期2010-05-01开始,以2010-05-10结束。如何在PHP中遍历所有这些日期?


当前回答

下面是另一个简单的实现

/**
 * Date range
 *
 * @param $first
 * @param $last
 * @param string $step
 * @param string $format
 * @return array
 */
function dateRange( $first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
    $dates = [];
    $current = strtotime( $first );
    $last = strtotime( $last );

    while( $current <= $last ) {

        $dates[] = date( $format, $current );
        $current = strtotime( $step, $current );
    }

    return $dates;
}

例子:

print_r( dateRange( '2010-07-26', '2010-08-05') );

Array (
    [0] => 2010-07-26
    [1] => 2010-07-27
    [2] => 2010-07-28
    [3] => 2010-07-29
    [4] => 2010-07-30
    [5] => 2010-07-31
    [6] => 2010-08-01
    [7] => 2010-08-02
    [8] => 2010-08-03
    [9] => 2010-08-04
    [10] => 2010-08-05
)

其他回答

如果你使用的是低于8.2版本的php,并且没有DatePeriod::INCLUDE_END_DATE const。我写了一个返回\DateTimeImmutable数组的方法。

这适用于开始日期在结束日期之前、相同或之后的情况。

    /**
     * @param DateTimeImmutable $start
     * @param DateTimeImmutable $end
     * @return array<\DateTimeImmutable>
     */
    public static function getRangeDays(\DateTimeImmutable $start, \DateTimeImmutable $end): array
    {
        $startDate = $start;
        $endDate = $end;
        $forwards = $endDate >= $startDate;
        $carryDate = $startDate;
        $days = [];
        while (true) {
            if (($forwards && $carryDate > $end) || (!$forwards && $carryDate < $end)) {
                break;
            }
            $days[] = $carryDate;

            if ($forwards) {
                $carryDate = $carryDate->modify('+1 day');
            } else {
                $carryDate = $carryDate->modify('- 1 day');
            }
        }

        return $days;
    }

下面是另一个简单的实现

/**
 * Date range
 *
 * @param $first
 * @param $last
 * @param string $step
 * @param string $format
 * @return array
 */
function dateRange( $first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
    $dates = [];
    $current = strtotime( $first );
    $last = strtotime( $last );

    while( $current <= $last ) {

        $dates[] = date( $format, $current );
        $current = strtotime( $step, $current );
    }

    return $dates;
}

例子:

print_r( dateRange( '2010-07-26', '2010-08-05') );

Array (
    [0] => 2010-07-26
    [1] => 2010-07-27
    [2] => 2010-07-28
    [3] => 2010-07-29
    [4] => 2010-07-30
    [5] => 2010-07-31
    [6] => 2010-08-01
    [7] => 2010-08-02
    [8] => 2010-08-03
    [9] => 2010-08-04
    [10] => 2010-08-05
)

这里有一个方法:

 $date = new Carbon();
 $dtStart = $date->startOfMonth();
 $dtEnd = $dtStart->copy()->endOfMonth();

 $weekendsInMoth = [];
 while ($dtStart->diffInDays($dtEnd)) {

     if($dtStart->isWeekend()) {
            $weekendsInMoth[] = $dtStart->copy();
     }

     $dtStart->addDay();
 }

$weekendsInMoth的结果是周末天数的数组!

<?php

    $start_date = '2015-01-01';
    $end_date = '2015-06-30';

    while (strtotime($start_date) <= strtotime($end_date)) {
        echo "$start_daten";
        $start_date = date ("Y-m-d", strtotime("+1 days", strtotime($start_date)));
    }

?>
$date = new DateTime($_POST['date']);
$endDate = date_add(new DateTime($_POST['date']),date_interval_create_from_date_string("7 days"));

while ($date <= $endDate) {
    print date_format($date,'d-m-Y')." AND END DATE IS : ".date_format($endDate,'d-m-Y')."\n";
    date_add($date,date_interval_create_from_date_string("1 days"));
}

你也可以这样迭代,$_POST['date']可以从你的应用程序或网站凹痕 而不是$_POST['date']你也可以把你的字符串放在这里"21-12-2019"。两者都可以。