Oihana PHP

subMonths.php

Table of Contents

Functions

subMonths()  : DateTimeImmutable
Returns a copy of a date shifted backwards by a number of months, clamping day overflow.

Functions

subMonths()

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

subMonths(DateTimeInterface $date, int $months) : DateTimeImmutable

A new DateTimeImmutable is returned ; the source date is never modified. Equivalent to addMonths( $date , -$months ) — see addMonths() for the day-clamping semantics.

Parameters
$date : DateTimeInterface

The source date.

$months : int

The number of months to subtract.

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

subMonths( new DateTimeImmutable( '2026-03-15' ) , 1 ) ; // 2026-02-15
subMonths( new DateTimeImmutable( '2026-03-31' ) , 1 ) ; // 2026-02-28 (clamped)
author

Marc Alcaraz (ekameleon)

since
1.2.0
Return values
DateTimeImmutable

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

On this page

Search results