PHP DateTime writing value out during a WHILE loop

David Morgan

This is probably something REALLY simple but its doing my head in!

I'm not a coder - so this is cobbled together from what I have learnt - that said it all works as-is except one weird problem....

I'm populating an array for exporting to JSON, but am adding all the dates that are missing from the SQL query.

EVERYTHING works EXCEPT the datetime variable in the 2nd "WHILE" loop!?!?

The loop works as expected - it iterates the correct number of times and adds the correct row to the array....

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

The output I am getting from the above is something like (see the pic at the end for real results!):

What I get ---> 26/10/19 27 ------ >Correct

What I get ---> 27/10/19 0 ------ >Correct

What I get ---> 27/10/19 0 ------ >Expecting "28/10/19 0"

What I get ---> 27/10/19 0 ------ >Expecting "29/10/19 0"

What I get ---> 30/10/19 25 ------ >Correct

What I get ---> 31/10/19 0 ------ >Correct

What I get ---> 01/11/19 45 ------ >Correct

So "$firstdate" IS incrementing, but when I write it out into the array it still has the old value? But only in the while loop?!!?

This is all dumped out into JSON and read into Google Charts - which displaysenter image description here

David Morgan

Ok - found the problem - just needed more coffee and looking at it fresh at 6am!

The "temp" array was only being created on the outer loop, not the inner.... hence the inner always had the old values!

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related