Skip to content

DateInterval issues with negative directions #16220

Open
@cjhewett

Description

@cjhewett

Description

I am going to put a few connected issues I have noticed around DateInterval and DatePeriod into a single report:

1: DateInterval->invert is "readonly"

DateInterval:::__construct() only allows positive $duration e.g. P1D not P-1D (I appreciate this is the correct behaviour as it is based around the ISO8601 which has no concept of negative periods). However... the DateInterval->invert which allows the period to be reversed can only be changed by manually setting that property and the docs explicitly say:

The available properties listed below depend on PHP version, and should be considered as readonly.

It would be good to be able to change this property safely.

2: DateInterval::createFromDateString allows negative periods

DateInterval::createFromDateString('-1 day') is accepted but this sets DateInterval->d = -1 rather than using the ::invert property. This is inconsistent with the always-positive periods which __construct allows.

I would expect it to:

  • Reject mixed positive and negative period values, what would +1 year -6 month even mean?
  • Accept if only negative periods passed but then set ::invert = 1 and the properties of ::y..f as +ve

3: DateInterval::createFromDateString('-1 day') leads to infinite loop in DatePeriod

$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-01'),
    \DateInterval::createFromDateString('-1 day'),
    new \DateTimeImmutable('2000-01-05'),
);

echo iterator_count($datePeriod); // stuck in infinite loop

4: DateInterval::createFromDateString('-1 day') still doesn't work if start and end reversed in DatePeriod

$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-05'),
    \DateInterval::createFromDateString('-1 day'),
    new \DateTimeImmutable('2000-01-01'),
);

echo iterator_count($datePeriod); // 0 (but would expect 4)

5: DatePeriod doesn't respect DateInterval->invert

$dateInterval = new \DateInterval('P1D');
$dateInterval->invert = 1;

# will not go backwards from high to low dates
$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-05'),
    $dateInterval,
    new \DateTimeImmutable('2000-01-01'),
);

echo iterator_count($datePeriod); // 0 (but would expect 4)

# incorrectly goes forward from low to high dates
$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-01'),
    $dateInterval,
    new \DateTimeImmutable('2000-01-05'),
);

echo iterator_count($datePeriod); // 4 (but would expect 0)

PHP Version

8.3.114

Operating System

Rocky 8

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions