Bug #72004 [NEW]: DateTime comparison unexpected behavior
| From: | mcloide1977 at gmail dot com | Date: | Mon, 11 Apr 2016 16:27:32 +0000 |
| Subject: | Bug #72004 [NEW]: DateTime comparison unexpected behavior | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-200482@lists.php.net to get a copy of this message | ||
From: mcloide1977 at gmail dot com
Operating system: Ubuntu 14.04.3 LTS
PHP version: 5.6.20
Package: Date/time related
Bug Type: Bug
Bug description:DateTime comparison unexpected behavior
Description:
------------
When comparing 2 DateTime objects using comparison operators such as
greater than, or less than, etc, you would expect that if DateTime
object 1 has a timestamp that is greater than DateTime object 2 that it
would return a truthful statement when being compared.
While running some unit tests I managed to get some failures on a
comparison test that simply did is $now >= $earlier. It, in all possible
variations tested, failed where $now was never greater than or equals
the $earlier DateTime object. That changed whenever there was a direct
timestamp comparison happening or when the getTimeStamp method from the
$now DateTime object.
Before submitting this bug I have checked this bug report
https://bugs.php.net/bug.php?id=68078 in which
I had the hope that
fixed the issue. It did not.
The following test script was ran under:
PHP 5.6.20-2+deb.sury.org~trusty+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend
Technologies
and under
PHP 5.6.19 (cli) (built: Mar 14 2016 11:55:51)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
With the same end result.
A copy of this test script can be found in:
https://gist.github.com/mcloide/eb7db1cc38fe4c6476388902bda724c3
Test script:
---------------
<?php
foreach (['EST', 'EDT', 'CST', 'CDT', 'PST',
'PDT'] as $tz) {
echo "---- TZ: $tz ---- \n";
$now = new DateTime('@' . (new DateTime('11:30 AM
EST'))->getTimeStamp());
$earlier = new DateTime('7 AM', new DateTimeZone($tz));
echo $now->format('r') . "\n";
echo $earlier->format('r') . "\n";
$mod = $now->setTimeZone(new DateTimeZone($tz));
echo $now->format('r') . "\n";
echo $earlier->format('r') . "\n";
echo (int)($earlier < $now) . "\n";
echo $now->getTimestamp() . "\n";
echo (int)($earlier < $now) . "\n";
echo "------------------\n\n";
}
Expected result:
----------------
When performing this comparison:
echo (int)($earlier < $now) . "\n";
Regardless if getTimeStamp method is called the end result should be
true.
---- TZ: CDT ----
Mon, 11 Apr 2016 16:30:00 +0000
Mon, 11 Apr 2016 07:00:00 -0600
Mon, 11 Apr 2016 10:30:00 -0600
Mon, 11 Apr 2016 07:00:00 -0600
1
1460392200
1
------------------
Actual result:
--------------
---- TZ: CDT ----
Mon, 11 Apr 2016 16:30:00 +0000
Mon, 11 Apr 2016 07:00:00 -0600
Mon, 11 Apr 2016 10:30:00 -0600
Mon, 11 Apr 2016 07:00:00 -0600
0
1460392200
1
------------------
--
Edit bug report at https://bugs.php.net/bug.php?id=72004&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72004&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72004&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72004&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72004&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72004&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72004&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72004&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72004&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72004&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72004&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72004&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72004&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72004&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72004&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72004&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72004&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72004&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72004&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72004&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72004&r=mysqlcfg