0

I have a problem in this code:

while ($end <= $to){
        $currentDates = array("from" => $start, "to"=>$end);
        $allDates[] = $currentDates;
        echo 'from: ', $currentDates["from"]->format("m-d-y"),'<br>';
        unset($currentDates);
        $start->add($intervalObj);
        $end->add($intervalObj);
    }

var_dump($allDates);

the echo in the loop shows the correct values but vardump shows the last dates to be added to the array in all the positions of the array

5
  • Can i ask what is the purpose of the loop ?? Are you trying to extract dates of a given range Commented Oct 15, 2012 at 16:40
  • It's a function the takes a start and end DateTime objects and interval type and interval amount and returns an array of DateTime objects based on those criteria. Commented Oct 15, 2012 at 16:44
  • what you get if var_dump($allDates) before foreach ? Commented Oct 15, 2012 at 16:46
  • good idea. I get the same thing I get in the foreach loop. So the problem is in the while loop. Commented Oct 15, 2012 at 16:49
  • When I say I get the same thing I mean the array is full of the same date repeated over and over. Commented Oct 15, 2012 at 16:59

1 Answer 1

1

I don't think you need 2 loops for that ....

The error is from your loop

while ($end <= $to){
                ^-------  This was never used

Also See

$currentDates = array("from" => $start, "to"=>$end);
         Not in the Condition  --^              ^---- To means something else 

You while can be as simple as

$start = new DateTime("2012-4-12");
$end = new DateTime("2012-12-12");
$dv = new DateInterval('P24D'); // Every 24 days

echo "<pre>";
while ( $start <= $end ) {
    echo "From ", $start->format('Y-m-d');
    $start->add($dv);
    echo " To ", $start->format('Y-m-d'), PHP_EOL;
}

Output

From 2012-04-12 To 2012-05-06
From 2012-05-06 To 2012-05-30
From 2012-05-30 To 2012-06-23
From 2012-06-23 To 2012-07-17
From 2012-07-17 To 2012-08-10
From 2012-08-10 To 2012-09-03
From 2012-09-03 To 2012-09-27
From 2012-09-27 To 2012-10-21
From 2012-10-21 To 2012-11-14
From 2012-11-14 To 2012-12-08
From 2012-12-08 To 2013-01-01
Sign up to request clarification or add additional context in comments.

3 Comments

The echo's are just for debugging the problem. The $to is used earlier in the function. This function takes in a start date end date interval type and an interval amount and returns an array of DateTime objects based on the supplied information. The echo in the while loop shows the correct dates but when I dump the $allDates array it shows the last date added for all positions in the array.
Ok which this can you see what you did wrong ?? or you still need help @Casey
@Baba I edited the question to, I hope, more clearly explain the problem.

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.