php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70810 DateInterval equals every other DateInterval
Submitted: 2015-10-29 09:50 UTC Modified: 2020-03-10 10:21 UTC
Votes:4
Avg. Score:4.0 ± 0.7
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: f dot bosch at genkgo dot nl Assigned: nikic (profile)
Status: Closed Package: Date/time related
PHP Version: 5.6.14 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: f dot bosch at genkgo dot nl
New email:
PHP Version: OS:

 

 [2015-10-29 09:50 UTC] f dot bosch at genkgo dot nl
Description:
------------
DateInterval always equals another DateInterval object while their inner values are different. This is not expected behaviour. Looks like this is the case for all PHP versions: https://3v4l.org/bQnf0. If you ask me, this bug is different then #70381 (Support comparing DateInterval objects).

Test script:
---------------
<?php

var_dump(new DateInterval('PT1H') == new DateInterval('P1D'));
var_dump(new DateInterval('PT1H') === new DateInterval('P1D'));
var_dump(new DateInterval('PT1H') , new DateInterval('P1D'));

Expected result:
----------------
bool(true)
bool(false)
object(DateInterval)#3 (0) { }
object(DateInterval)#4 (0) { }

Actual result:
--------------
bool(false)
bool(false)
object(DateInterval)#3 (0) { }
object(DateInterval)#4 (0) { }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-29 09:52 UTC] f dot bosch at genkgo dot nl
Actual and expected result are switched (sorry for that).
 [2016-05-16 12:07 UTC] php dot net at anthonychambers dot co dot uk
I've found a similar issue: https://3v4l.org/TrPvp

What I found was that if I compared two DateInterval objects, they would always return false.

That is, until I actually get some data from one of the objects (in my case I just var_export($interval, true) and discard the output) at which time everything starts to return true. Also weird.

If I inspect both intervals first however, suddenly it all evaluates correctly! I've tested this in PHP 7.0.0 and PHP 7.0.6. 3v4l.org suggests that it will work back to 5.5.8 though
 [2016-12-15 02:59 UTC] rupert dot chen at gmail dot com
I believe I have run into the same issue. It appears a new DateInterval is considered "0", or perhaps not fully initialized internally. After "certain interactions" with the DateInterval, for example as an arg to print_r(), it appears to become fully initialized.

The provided example yields strange behavior in the two version of PHP at my disposal:

PHP 5.4.45 (cli) (built: Sep  4 2015 15:40:44) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans


PHP 5.5.36 (cli) (built: May 29 2016 01:07:06) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies


Test Script
-----------

<?php
$x = new DateTime('2016-01-01 00:00:00 UTC');
$y = new DateTime('2016-01-01 00:00:15 UTC');

// Should be 0s
$z1 = $x->diff($x);
// Should be 15s
$z2 = $x->diff($y);
// Should be 30s
$z3 = new DateInterval('PT30S');

echo "No inspections\n";
var_dump($z1 == $z2);
var_dump($z1 == $z3);
var_dump($z2 == $z3);
var_dump($z1 > $z2);
var_dump($z2 > $z3);

echo "Inspect \$z1\n";
print_r($z1);
var_dump($z1 == $z2);
var_dump($z1 == $z3);
var_dump($z2 == $z3);
var_dump($z1 > $z2);
var_dump($z2 > $z3);

echo "Inspect \$z2\n";
print_r($z2);
var_dump($z1 == $z2);
var_dump($z1 == $z3);
var_dump($z2 == $z3);

// After inspecting all DateIntervals, these are the expected results.
echo "Inspect \$z3\n";
print_r($z3);
var_dump($z1 == $z2);
var_dump($z1 == $z3);
var_dump($z2 == $z3);
var_dump($z1 > $z2);
var_dump($z2 > $z3);



Script Output
-------------

No inspections
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
Inspect $z1
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 0
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
Inspect $z2
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 15
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 0
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
Inspect $z3
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 30
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
 [2019-04-18 13:49 UTC] nikic@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: nikic
 [2020-03-10 10:21 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-03-10 10:21 UTC] cmb@php.net
This is fixed as of PHP 7.4.0[1], so I'm closing this ticket.

[1] <https://3v4l.org/bQnf0>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jun 02 18:01:26 2025 UTC