The 7th Annual China PHP Conference

DateTimeImmutable::add

(PHP 5 >= 5.5.0, PHP 7)

DateTimeImmutable::add Добавляет указанное количество дней, месяцев, лет, часов, минут и секунд

Описание

public DateTimeImmutable::add ( DateInterval $interval ) : DateTimeImmutable

Данный метод похож на DateTime::add(), но работает с DateTimeImmutable.

add a note add a note

User Contributed Notes 1 note

up
-2
Gunnar Bernstein
6 months ago
Please note, add() works a litte different than for DateTime-Objects.

Since DateTimeImmutable is in fact immutable. A line like this will not work:

    $di = new DateTimeImmutable("2018-12-12T10:00:00");
    $di->add(new DateInterval('PT45M')); // $di unchanged !!!

compared to

    $dt = new DateTime("2018-12-12T10:00:00");
    $dt->add(new DateInterval('PT45M')); // added 45 minutes

so you need to write

    $di = new DateTimeImmutable("2018-12-12T10:00:00");
    $di = $di->add(new DateInterval('PT45M')); // $di now changed
To Top