2

I want to use PHP to populate an array starting with today's date and going several days into the future. When I tried the following below all of the columns contain "2013-11-18." I have been toying with it for 2 hours, but to no avail. What am I missing?

//Get "Day 0", today if undefined
if(isset($_GET['DAY0']) == TRUE){
    $day0 = new DateTime($_GET['DAY0']);
} else {
    $day0 = new DateTime('today');
}

    // save day0 + 7 days into into dayArray
    $dayArray[0] = $day0;
    for($i=1; $i<8; $i++){
        $day0->modify('+1 day');
        $dayArray[i]= $day0;
    }

    echo "<tr>"; 
    for ($i = 0; $i < 7; $i++) {
        echo "<th>".$dayArray[i]->format('Y-m-d')."</th>";
    }
    echo "</tr>";

4 Answers 4

2

Objects are passed by reference. You are assigning multiple references to the same object in your array.

If you really need all the datetime objects in the array, you could do something like this

$interval = new DateInterval('P1D');
$start = new DateTime('today');

$dayArray = [clone $start];

for ($i = 1; $i < 8; $i++) {
    $dayArray[] = clone $start->add($interval);
}

Or you could just store the formatted dates as already suggested.

$interval = new DateInterval('P1D');
$start = new DateTime('today');

$dayArray = [$start->format('Y-m-d')];

for ($i = 1; $i < 8; $i++) {
    $dayArray[] = $start->add($interval)->format('Y-m-d');
}
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please tell me how to do that because I can't figure it out... sorry
@Rob, $dayArray = [clone $start]; is not ok, what language? $dayArray = [$start->format('Y-m-d')]; also not ok. What's with [ and ] on both of them?
That is short array syntax added in php 5.4.
0

Replace two of your $dayArray[i] with $dayArray[$i]

2 Comments

I tried that and it produced a fatal error... I think because of the format thing.
Oh ya, the major problem is in @Rob's answer, but this one should also be corrected.
0

You could save timestamps:

// save day0 + 7 days into into dayArray
$dayArray[0] = $day0->format('U');
for($i=1; $i<8; $i++){
    $day0->modify('+1 day');
    $dayArray[$i] = $day0->format('U');
}

echo "<tr>"; 
for ($i = 0; $i < 7; $i++) {
    echo "<th>".date('Y-m-d', $dayArray[$i])."</th>";
}

Comments

0

You can create a DatePeriod like so:

    if(isset($_GET['DAY0']) == TRUE){
        $day0 = new DateTime($_GET['DAY0']);
    } else {
        $day0 = new DateTime('today');
    }

    $enddate = new DateTime();

    $period = new DatePeriod(
            $day0,
            new DateInterval('P1D'),
            $enddate->add(new DateInterval('P7D'))
    );

    echo "<tr>";
      foreach ($period as $datetime) {
         echo "<th>".datetime->format('Y-m-d')."</th>";

      }
    echo "</tr>";

2 Comments

Thanks for your answer, but I wanted to store each date in an array because I loop through them a second time to populate the table...
You can use iterator_to_array() function to convert the period into an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.