I have this pretty simple code:
$start_date = new DateTime($post['start_date']);
$end_date = $start_date->add(new DateInterval('P6M'));
echo $start_date->getTimestamp(); // 1351836000
echo $end_date->getTimestamp(); // 1351836000
Of course, both end up as the same timestamp because adding the date interval affects the original $start_date. So how do I go about this so I can keep the original $start_date yet add 6 months to it in another variable?
I tried this with no luck:
$start_date = new DateTime($post['start_date']);
$start_date_actual = $start_date;
$end_date = $start_date_actual->add(new DateInterval('P6M'))->getTimestamp();
Variables hold references to objects, not the objects themselves. So assignment just gets you more variables pointing to the same object, not multiple copies of the object.
If you want a copy, use the
clonekeyword: