Oihana PHP

addYears.php

Table of Contents

Functions

addYears()  : DateTimeImmutable
Returns a copy of a date shifted by a number of years, clamping day overflow.

Functions

addYears()

Returns a copy of a date shifted by a number of years, clamping day overflow.

addYears(DateTimeInterface $date, int $years) : DateTimeImmutable

A new DateTimeImmutable is returned ; the source date is never modified. Delegates to addMonths( $date , $years * 12 ), so a leap day clamps the same way : 2028-02-29

  • 1 year lands on 2029-02-28, 2029 not being a leap year, rather than overflowing to 2029-03-01.
Parameters
$date : DateTimeInterface

The source date.

$years : int

The number of years to add (negative to subtract).

Tags
example
use function oihana\core\date\addYears;

addYears( new DateTimeImmutable( '2026-01-15' ) , 1 )  ; // 2027-01-15
addYears( new DateTimeImmutable( '2028-02-29' ) , 1 )  ; // 2029-02-28 (clamped, 2029 is not a leap year)
addYears( new DateTimeImmutable( '2026-01-15' ) , -1 ) ; // 2025-01-15
author

Marc Alcaraz (ekameleon)

since
1.2.0
Return values
DateTimeImmutable

A new immutable date shifted by $years years, day-clamped.

On this page

Search results