DrupalDateTime (and possibly DateTimePlus) relative comparison is broken in PHP 7.3 using relational operators, but DateTime relative comparison is working as intended

Created on 29 May 2019, over 5 years ago
Updated 28 May 2024, 7 months ago

The comparison of two DrupalDateTime object instances using relational operators in PHP 7.3 does not produce the same result as two regular DateTime object instances in some circumstances. See the following example:

use Drupal\Core\Datetime\DrupalDateTime;

$dt_now = new DateTime();
$dt_past = new DateTime('-1 day');
$ddt_now = new DrupalDateTime();
$ddt_past = new DrupalDateTime('-1 day');

var_export($dt_past->format('F a')); // "May pm"
var_export($dt_now < $dt_past); // false
var_export($dt_now->format('c')); // 2019-05-29T15:43:02-04:00
var_export($dt_past->format('c')); // 2019-05-28T15:43:02-04:00

var_export($ddt_past->format('F a')); // "May pm"
var_export($ddt_now < $ddt_past); // true
var_export($ddt_now->format('c')); // 2019-05-29T15:43:02-04:00
var_export($ddt_past->format('c')); // 2019-05-28T15:43:02-04:00

In PHP 7.3, the relative comparison of user-defined objects results in the recursive, short-circuit comparison of each instance property's value (see this link). Considering this information, it logically follows that $ddt_past->format('F a') should then cause $ddt_now->formatTranslationCache[] to alter how the $ddt_past object instance is compared with other instances of the same type.

Ideally, DrupalDateTime (and DateTimePlus) should behave identically to DateTime with respect to the use of relational operators to compare two object instances.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
DatetimeΒ  β†’

Last updated 3 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States clayfreeman Paragould, AR

Live updates comments and jobs are added and updated live.
  • PHP 7.3

    The issue particularly affects sites running on PHP version 7.3.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    @clayfreeman, thanks for pointing this problem out and providing a patch.

    This issue is about a problem with PHP 7.3, which is EOL and is not used by a currently supported version of Drupal. Therefore I am closing this issue.

  • Status changed to Active 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom jonathanshaw Stroud, UK

    @quietone I don't see any discussion in this issue about whether this is or is not a problem with php 7.4+ or php 8. Maybe it was first reported when php 7.3 was the current version of php. The test coverage provided in the patch would seem useful even if it was not a current problem.

Production build 0.71.5 2024