This is my controller
$entretien = new Entretien();
$form3 = $this->createForm(EntretienType::class, $entretien); // entretien type has a attribute debutRdv
$form3->add('duree', TimeType::class, [
'placeholder' => [
'hour' => 'Heure', 'minute' => 'Minute', 'second' => 'Seconde',
],
'with_seconds' => true,
"mapped" => false,
]);
$form3->handleRequest($request);
if($form3->isSubmitted())
{
$debutRdv= $entretien->getDebutRdv(); // getdata from form
$duree =$form3->get("duree")->getData();
$duree = $duree->format('\P\TH\Hi\Ms\S');
/*------------Here the probleme--------*/
$finRdv = $debutRdv->add(new \DateInterval($duree));
/*-------------------------------------*/
...
The problem is when I write $debutRdv->add(...) the attribute $debutRdv changes to $finRdv like I've done $debutRdv = $finRdv;
For example $debutRdv: "2019-03-25 16:30:00" $duree : "0:30:00"
When I write this
$finRdv = $debutRdv->add(new \DateInterval($duree));
$duree and $debutRdv change to 2019-03-25 17:00:00 but I only want $duree got 2019-03-25 17:00:00
I want to solve this problem because $entretien->getDebutRdv() also changes to 2019-03-25 17:00:00 Like I have done $entretien->setDebutRdv($duree)
This is the default behaviour of \DateTime
instances: add
method changes object it is called from. If you don't want $debutRdv
to be changed, you can clone it to $finRdv
and apply add
:
$finRdv = clone $debutRdv;
// see, here's no `=` as `add()` will directly change `$finRdv`
$finRdv->add(new \DateInterval($duree));