通过 strtotime 循环开始日期和结束日期不起作用 PHP

Loop over start date and end date through strtotime not working PHP

我有一个这样的数组:

Array
(
    [0] => Array
        (
            [start_date] => 2010-09-16
        )

    [1] => Array
        (
            [end_date] => 2010-10-16
        )

    [2] => Array
        (
            [start_date] => 2011-12-24
        )

    [3] => Array
        (
            [end_date] => 2012-01-23
        )

    [4] => Array
        (
            [start_date] => 2012-11-10
        )

    [5] => Array
        (
            [end_date] => 2012-12-10
        )

    [6] => Array
        (
            [start_date] => 2013-05-14
        )

    [7] => Array
        (
            [end_date] => 2013-06-13
        )

    [8] => Array
        (
            [start_date] => 2014-10-20
        )

    [9] => Array
        (
            [end_date] => 2014-11-19
        )

);

我已经编写了以下 foreach 循环来遍历数组,在 foreach 循环中我想通过使用 while loopstrtotime 获取开始日期和结束日期之间的日期(引用 this 篇文章)

foreach ($dateArray as $key => $data) {
            $start_date = $dateArray[$key]['start_date'];
            $end_date = $dateArray[$key]['end_date'];

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

但是加载页面会花费大量时间并显示错误的输出,例如:

1970-01-02
1970-01-03
1970-01-04
1970-01-05
1970-01-06
1970-01-07
.
.
.

等等...

但是如果我将硬代码值传递给它,例如:

foreach ($dateArray as $key => $data) {
            $start_date = $dateArray[$key]['start_date'];
            $end_date = $dateArray[$key]['end_date'];

            while (strtotime('2009-12-06') <= strtotime('2020-12-31')) {
                echo "$start_date\n";
                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($start_date)));
            }
        }

然后它工作正常并显示正确的结果。

如有任何帮助,我们将不胜感激。

$array = array_chunk($dateArray,2);
foreach ($array as $key => $data) {
        $start_date = data[0]['start_date'];
        $end_date =data[1]['end_date'];

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

你可以这样使用

foreach ($dateArray as $key => $data) {

    if ($key % 2 == 0) {
        if ($end_date != '') {
            $end_date = '';
        }
    }


    if (isset($dateArray[$key]['start_date'])) {
        $start_date = $dateArray[$key]['start_date'];
    }

    if (isset($dateArray[$key]['end_date'])) {
        $end_date = $dateArray[$key]['end_date'];
    }


    if ($start_date != '' && $end_date != '') {
        while (strtotime($start_date) <= strtotime($end_date)) {
            echo "$start_date\n";
            $start_date = date("Y-m-d", strtotime("+1 day", strtotime($start_date)));
        }
    }
}

这里的问题是您正在尝试访问您无权访问的值。您为每次迭代尝试做的是:

[0]['StartDate']

[0]['EndDate']

并为循环的每次迭代执行此操作,从您的数组结构来看,它应该是:

[0]['StartDate']

[1]['EndDate']

如果您始终知道结束日期在它的下一次迭代中,您可以这样做:

for($x = 0; $x < count($dateArray); $x = $x + 2) 
{
     if(isset($dateArray[$x + 1]))
     {
        $start_date = $dateArray[$x]['start_date'];
        $end_date = $dateArray[$x + 1]['end_date'];

        //preform calculation
        while (strtotime($start_date) <= strtotime($end_date)) {
            echo "$start_date\n";
            $start_date = date("Y-m-d", strtotime("+1 day",                 strtotime($start_date)));
        }

     }
}

但如果您想使用当前代码,您应该以不同的方式存储数组:

 [0] => Array
    (
        [start_date] => 2010-09-16
        [end_date] => 2010-10-16
    )