Date difference and adding time

DateTimeImmutable never changes the original, which avoids many bugs.

PHP · Date difference and adding timeIntermediate
$start = new DateTimeImmutable('2026-01-15');
$end   = new DateTimeImmutable('2026-11-03');

$diff = $start->diff($end);
echo $diff->days;                           // total days: 292
echo $diff->m, ' months ', $diff->d, ' days';

$due  = $start->add(new DateInterval('P30D'));       // +30 days
echo $due->format('Y-m-d');                          // 2026-02-14
echo $start->modify('last day of this month')->format('Y-m-d');   // 2026-01-31
datetimediffinterval