Skip to content

Inconsistency with DatePeriod and P1M when the start date is the last day of month #17537

@alexandre-daubois

Description

@alexandre-daubois

Description

The following code:

<?php

$period = new DatePeriod(
    new DateTimeImmutable('2025-01-31'),
    new DateInterval('P1M'),
    new DateTimeImmutable('2025-04-30'),
);

foreach ($period as $date) {
    echo $date->format('Y-m-d') . PHP_EOL;
}

Resulted in this output:

2025-01-31
2025-03-03
2025-04-03

But I expected this output instead:

2025-01-31
2025-02-28
2025-03-31

It seems PHP's having a hard time when the start month has more days than the next one.

Thanks!

PHP Version

PHP 8.3.16

Operating System

macOS 15.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions