Skip to content

Commit d7f5f1e

Browse files
bor0nikic
authored andcommitted
Fix bug #65548: Comparison for DateTimeImmutable doesn't work
1 parent d209c36 commit d7f5f1e

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ PHP NEWS
3636
. Fixed bug #65458 (curl memory leak). (Adam)
3737

3838
- Datetime:
39+
. Fixed bug #65548 (Comparison for DateTimeImmutable doesn't work).
40+
(Boro Sitnikovski)
3941
. Fixed bug #65554 (createFromFormat broken when weekday name is followed
4042
by some delimiters). (Valentin Logvinskiy, Stas).
4143
. Fixed bug #65564 (stack-buffer-overflow in DateTimeZone stuff caught

ext/date/php_date.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,27 +2142,21 @@ static zval* date_clone_immutable(zval *object TSRMLS_DC)
21422142

21432143
static int date_object_compare_date(zval *d1, zval *d2 TSRMLS_DC)
21442144
{
2145-
if (Z_TYPE_P(d1) == IS_OBJECT && Z_TYPE_P(d2) == IS_OBJECT &&
2146-
instanceof_function(Z_OBJCE_P(d1), date_ce_date TSRMLS_CC) &&
2147-
instanceof_function(Z_OBJCE_P(d2), date_ce_date TSRMLS_CC)) {
2148-
php_date_obj *o1 = zend_object_store_get_object(d1 TSRMLS_CC);
2149-
php_date_obj *o2 = zend_object_store_get_object(d2 TSRMLS_CC);
2150-
2151-
if (!o1->time || !o2->time) {
2152-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to compare an incomplete DateTime object");
2153-
return 1;
2154-
}
2155-
if (!o1->time->sse_uptodate) {
2156-
timelib_update_ts(o1->time, o1->time->tz_info);
2157-
}
2158-
if (!o2->time->sse_uptodate) {
2159-
timelib_update_ts(o2->time, o2->time->tz_info);
2160-
}
2161-
2162-
return (o1->time->sse == o2->time->sse) ? 0 : ((o1->time->sse < o2->time->sse) ? -1 : 1);
2145+
php_date_obj *o1 = zend_object_store_get_object(d1 TSRMLS_CC);
2146+
php_date_obj *o2 = zend_object_store_get_object(d2 TSRMLS_CC);
2147+
2148+
if (!o1->time || !o2->time) {
2149+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
2150+
return 1;
2151+
}
2152+
if (!o1->time->sse_uptodate) {
2153+
timelib_update_ts(o1->time, o1->time->tz_info);
2154+
}
2155+
if (!o2->time->sse_uptodate) {
2156+
timelib_update_ts(o2->time, o2->time->tz_info);
21632157
}
21642158

2165-
return 1;
2159+
return (o1->time->sse == o2->time->sse) ? 0 : ((o1->time->sse < o2->time->sse) ? -1 : 1);
21662160
}
21672161

21682162
static HashTable *date_object_get_gc(zval *object, zval ***table, int *n TSRMLS_DC)

ext/date/tests/bug65548.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Test for bug #65548: Comparison for DateTimeImmutable doesn't work
3+
--CREDITS--
4+
Boro Sitnikovski <buritomath@yahoo.com>
5+
--INI--
6+
date.timezone = UTC
7+
--FILE--
8+
<?php
9+
$iToday = new DateTimeImmutable('today');
10+
$iTomorrow = new DateTimeImmutable('tomorrow');
11+
12+
$mToday = new DateTime('today');
13+
$mTomorrow = new DateTime('tomorrow');
14+
15+
var_dump($iToday < $iTomorrow);
16+
var_dump($iToday == $iTomorrow);
17+
var_dump($iToday > $iTomorrow);
18+
19+
var_dump($iToday == $mToday);
20+
var_dump($iToday === $mToday);
21+
22+
var_dump($iToday < $mTomorrow);
23+
var_dump($iToday == $mTomorrow);
24+
var_dump($iToday > $mTomorrow);
25+
?>
26+
--EXPECT--
27+
bool(true)
28+
bool(false)
29+
bool(false)
30+
bool(true)
31+
bool(false)
32+
bool(true)
33+
bool(false)
34+
bool(false)

0 commit comments

Comments
 (0)