I wrote this function
$today=new \DateTime(date('Y-m-d'));
$tomorrow=new \DateTime();
$tomorrow=$today->modify('+1 day');
$interval= new \DateInterval('P7D');
$enDt= date_add($today,$interval);
var_dump($tomorrow->format('d-m-Y'), $enDt->format('d-m-Y'));
and I expected $tomorrow should be different from $enDt, because, in the first case, I increase $today of 1 day, and in the second case $enDt of 7 , but in real-world result is $enDT equals $tomorrow.
this is another version, with same result:
$today=new \DateTime(date('Y-m-d'));
$tomorrow=new \DateTime();
$interval1= new \DateInterval('P1D');
$tomorrow=date_add($today,$interval1);
$interval2= new \DateInterval('P7D');
$enDt= date_add($today,$interval2);
var_dump($tomorrow,$enDt);
var_dump($tomorrow->format('d-m-Y'), $enDt->format('d-m-Y'));
The
DateTimeclass is mutable. It means that, when you callmodify()oradd()on it, the object is modified. So, in your example,$todayis modified.You can use
DateTimeImmutableinstead ofDateTimeto work with immutable dates.Result: