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

时间:2016-01-08 06:49:38

标签: php arrays date strtotime

我有这样的数组:

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)));
            }
        }

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

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

$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)));
        }
    }

答案 1 :(得分:1)

你可以这样使用

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)));
        }
    }
}

答案 2 :(得分:1)

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

[0] ['StartDate']

[0] ['EndDate']

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

[0] [ '开始日期']

[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
    )