date_add() changing 2 variables in PHP rather than 1
dateadd, php
Solution
The `clone` keyword is what you need.
$date2 = clone $date1;
When an object is cloned, a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.
If your object `$date2` holds a reference to another object `$date1` which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.
`Source`
Problem
Here is an example of the code I used: ``` <?php date_default_timezone_set("Europe/London"); $date1 = date_create("2014-04-05"); $date2 = $date1; date_add($date2, new DateInterval("P1M")); echo "Date 1: ".date_format($date1, "Y-m-d")."<br/>"; echo "Date 2: ".date_format($date2, "Y-m-d")."<br/>"; ?> ``` The result for this would be: ``` Date 1: 2014-05-05 Date 2: 2014-05-05 ``` I was expecting the result of: ``` Date 1: 2014-04-05 Date 2: 2014-05-05 ``` How can I get the expected result and fix this? I can only use PHP, HTML and CSS so no jQuery or Javascript please.