Skip to content

Return ChronosInterval from Chronos::diff() #511

@dereuromark

Description

@dereuromark

Following discussion in #495, this is a tracking issue for breaking changes to consider for 4.x.

Proposed Changes

Return ChronosInterval from Chronos::diff()

Currently Chronos::diff() returns a native DateInterval. In 4.x, it should return ChronosInterval instead to provide:

  • ISO 8601 duration formatting via __toString()
  • Convenience methods like totalSeconds(), totalDays(), isZero(), isNegative()
  • Arithmetic methods add(), sub()
  • toDateString() for strtotime-compatible output
  • toNative() for backwards compatibility when a DateInterval is needed

Migration Path

Users who need a DateInterval can call ->toNative() on the result:

// Before (3.x)
$interval = $date1->diff($date2);

// After (4.x) - if DateInterval is needed
$interval = $date1->diff($date2)->toNative();

// After (4.x) - using new features
$interval = $date1->diff($date2);
echo $interval; // "P1Y2M3D"
echo $interval->totalDays();

References

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions