Ok i got a weird issue here.
Im trying to calculate the old date and the new date. However i spotted a issue that is unexplainable by me.
The setup :
I have 2 new dates set on today.
$tempstamp = new DateTime();
$newstamp = new DateTime();
Now, i edit the newstamp into a new date useing :
if ($newstamp ->format('His') > 120000) $newstamp ->modify('+7 day');
else $newstamp ->modify('+4 day');
Which works perfectly. However now comes the weird part.
When i dump both values before and after it the tempstamp gives a unexplainable value :
case '2':
$tempstamp = $newstamp;
dump($tempstamp->format('m-d')); // 02-19 - CORRECT
dump($newstamp->format('m-d')); // 02-19 - CORRECT
if ($newstamp->format('His') > 120000) $newstamp->modify('+7 day');
else $newstamp->modify('+4 day');
dump($tempstamp->format('m-d')); // 02-26 - FALSE
dump($newstamp->format('m-d')); // 02-26 - CORRECT
*NOTE: dump is our way of var_dumping*
The Question:
Can someone explain why tempstamp is getting editted as well while i only edit newstamp?
PHP copies objects by reference, not by value. So
$tempstamp = $newstamp;is now making$tempstampand$newstamppoint to the same object. To avoid this you need to useclone:$tempstamp = clone $newstamp;