PHP DateTime在WHILE循环中写出值

这可能真的很简单,但是却引起了我的注意!

我不是编码人员-所以从我所学到的知识中可以看出来-这说得通,除了一个奇怪的问题以外,所有功能都按原样工作。...

我正在填充用于导出到JSON的数组,但是正在添加SQL查询中缺少的所有日期。

除第二个“ WHILE”循环中的datetime变量外,其他所有操作都有效!?!?

循环按预期方式工作-迭代正确的次数并将正确的行添加到数组中。...

            while ($nt = $result->fetch_assoc())
            {
                $temp = array();
                $day = new DateTime();
                $day->setTimestamp(strtotime($nt['day']));
                $day->format('U = d/m/Y');
                if ($day < $firstdate) {
                    $firstdate = $day;
                }
                // WHILE LOOP INCREMENTS CORRECTLY... so $firstdate is incrementing....
                while ($day > $firstdate) {
                    // THIS LINE DOEsnT SHOW THE INCREMENTED DATE?
                    $temp[] = array('v' => $firstdate->format('d/m/y'),'f' =>NULL);
                    $temp[] = array('v' => intval(0),'f' =>NULL);
                    $rows[] = array('c' => $temp);
                    $firstdate->add(new DateInterval('P1D'));
                }
                // THIS LINE DISPLAYS IT CORRECTLY!
                $temp[] = array('v' => $firstdate->format('d/m/y'),'f' =>NULL);
                $temp[] = array('v' => intval($nt['output']),'f' =>NULL);
                $rows[] = array('c' => $temp);
                $firstdate->add(new DateInterval('P1D'));
            }

我从上面获得的输出类似(实际结果请参见最后的图片!):

我得到的---> 19/10/26 27 ------>正确

我得到的---> 19 27/10/0 ------>正确

我得到的---> 19/10/19 0 ------>期待“ 28/10/19 0”

我得到的---> 19/10/19 0 ------>期待“ 29/10/19 0”

我得到的---> 19/10/30 25 ------>正确

我得到的---> 19/10/31 0 ------>正确

我得到的---> 19/01/11 45 ------>正确

因此“ $ firstdate”正在递增,但是当我将其写到数组中时,它仍然具有旧值吗?但是只有在while循环中吗?!

所有内容都转储到JSON中并读入Google图表-显示

PHP DateTime在WHILE循环中写出值

w5201021965 回答:PHP DateTime在WHILE循环中写出值

好吧-发现了问题-刚需要更多咖啡,并在凌晨6点新鲜地看着它!

“临时”数组仅在外部循环上创建,而不是在内部循环上创建。...因此,内部总是具有旧值!

                 while ($nt = $result->fetch_assoc()) {

                    //$temp = array();          <-----  DOH!
                    $day = new DateTime();
                    $day->setTimestamp(strtotime($nt['day']));
                    $day->format('d/m/y');
                    if ($day < $firstdate) {
                        $firstdate = $day;
                    }

                    while ($firstdate < $day) {
                        $temp = array();         // Needed to be here!
                        $temp[] = array('v' => $firstdate->format('d/m/y'),'f' =>NULL);
                        $temp[] = array('v' => intval(0),'f' =>NULL);
                        $rows[] = array('c' => $temp);
                        $firstdate->add(new DateInterval('P1D'));

                    }

                    $temp = array();       // Needed to be here as well!
                    $temp[] = array('v' => $firstdate->format('d/m/y'),'f' =>NULL);
                    $temp[] = array('v' => intval($nt['output']),'f' =>NULL);
                    $rows[] = array('c' => $temp);
                    $firstdate->add(new DateInterval('P1D'));
                }

本文链接:https://www.f2er.com/3141200.html

大家都在问