Angelin Calu
Angelin Calu

Reputation: 1915

Carbon initial date value

Maybe I'm not seeing the forest because of the trees but here's what I am facing:

$week_start = new Carbon();
$week_start->setISODate($year,$week);  //2016 , 21

$init = $week_start; //I am assigning the datetime created to $init variable (1)

$min_sqldate = $week_start->toDateString(); //string of date
$max_sqldate = $week_start->addDays(6)->toDateString(); //string of date adding 6 days

At this point $init variable already has a value of $max_sqldate. (2)

How is this possible? How should I keep my initial variable so I can use it later on ?

Value of $init in case (1):

 Carbon {#328 ▼
      +"date": "2016-05-23 16:58:36.000000"
      +"timezone_type": 3
      +"timezone": "Europe/Helsinki"
    }

Value of $init in case (2):

 Carbon {#328 ▼
      +"date": "2016-05-29 17:00:34.000000"
      +"timezone_type": 3
      +"timezone": "Europe/Helsinki"
 }

Upvotes: 0

Views: 456

Answers (1)

iainn
iainn

Reputation: 17417

Carbon provides a copy() method that will return a "fresh" copy of the instance, e.g.

$init = $week_start->copy();

By default in PHP, when copying an object using equals, you won't end up with an independent copy. You can also work around this using the clone keyword.

See http://php.net/manual/en/language.oop5.references.php

Upvotes: 2

Related Questions