1
$start  = $this->getWeekRange($date);
$end    = $start->modify("+6 days");

echo $start->format("Y-m-d");
echo $end->format("Y-m-d"); 
exit();

OUTPUT:

2013-12-08
2013-12-08

it should be

2013-12-02
2013-12-08

why is that both $start and $end has the same value? even though I've already assigned the value on the $start variable before modifying it then assigning it to $end.

1
  • var_dump($start, $end); Commented Dec 7, 2013 at 12:07

2 Answers 2

2

Objects are assigned by reference in PHP (and many other languages).

This means that $end and $start are pointing to the same object. In order to get a clone of that object you have to use clone:

$end = clone $start;

Now you have a seperate object in $end which has the same properties as $start; until you call methods or modify one of them.

For your example you should put the above line in the second line of your example and modify this line:

$end = $start->modify("+6 days");

to:

$end->modify("+6 days");
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I haven' t seen a diffrent way of doing this so far. Additionally using pure language approaches is always good.
0

try this:

$start  = $this->getWeekRange($date);
$end    = $start;
$end->modify("+6 days");

echo $start->format("Y-m-d");
echo $end->format("Y-m-d"); 
exit();

Comments

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.