Documentación offline PHP master

DateTimeImmutable::add

master Ver versión oficial en línea Licencia CC-BY-3.0Descargado el 2026-08-02

En esta página

DateTimeImmutable::add

Devuelve un nuevo objeto, con una cantidad añadida de días, meses, años, horas, minutos y segundos

Descripción#

#[\NoDiscard(message: "as DateTimeImmutable::add() does not modify the object itself")] public DateTimeImmutable::add(DateInterval $interval): DateTimeImmutable
```php

Crea un nuevo objeto `DateTimeImmutable`, y añade el objeto `DateInterval` especificado para representar el nuevo valor.

## Parámetros

`interval`  
Un objeto `DateInterval`

## Valores devueltos

Retorna un nuevo objeto `DateTimeImmutable` con los datos modificados.

## Ejemplos

Ejemplo de `DateTimeImmutable::add`

Estilo orientado a objetos
add(new DateInterval('P10D')); echo $newDate->format('Y-m-d') . "\n"; ?>
Más ejemplos de `DateTimeImmutable::add`
add(new DateInterval('PT10H30S')); echo $newDate->format('Y-m-d H:i:s') . "\n"; $date = new DateTimeImmutable('2000-01-01'); $newDate = $date->add(new DateInterval('P7Y5M4DT4H3M2S')); echo $newDate->format('Y-m-d H:i:s') . "\n"; ?>
El ejemplo anterior mostrará:

    2000-01-01 10:00:30
    2007-06-05 04:03:02

Tenga cuidado al agregar meses
add($interval); echo $newDate1->format('Y-m-d') . "\n"; $newDate2 = $newDate1->add($interval); echo $newDate2->format('Y-m-d') . "\n"; ?>

```php

El ejemplo anterior mostrará:

2001-01-31
2001-03-03

Véase también#

DateTimeImmutable::sub

DateTimeImmutable::diff

DateTimeImmutable::modify