PHP putting four dates in an array from a while loop -


using below while loop, 4 dates in echo, need.

$data = [];  $start = carbon::createfromdate('2016', '04', '01'); $end = carbon::createfromdate('2017', '04', '01');  while($start < $end) {     $data[] = $start;      echo $start;      $start->addmonths('3'); } 

output:

2016-04-01 2016-07-01 2016-10-01 2017-01-01 

but when dump $data[] array, 4 collections, each same date:

2017-04-01 

what doing wrong? want put above 4 dates in array.

you're assigning object instance ($start) each array entry (and objects "by reference"), , modifying object instance, same object instance being changed wherever.... assign "clone" array

while($start < $end) {     $data[] = clone $start;     echo $start;     $start->addmonths('3'); } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -