Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ PHP NEWS
. Fixed bug GH-20426 (Spoofchecker::setRestrictionLevel() error message
suggests missing constants). (DanielEScherzer)
. Added grapheme_strrev (Yuya Hamada)
. IntlDateFormatter::format() now accepts numeric-castable objects as
date/time values. (Weilin Du)

- JSON:
. Enriched JSON last error / exception message with error location.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ PHP 8.6 UPGRADE NOTES
. gmp_powm() modulo-by-zero now raises a DivisionByZeroError whose
message includes the function name and argument index ($modulus).

- Intl:
. IntlDateFormatter::format() now accepts objects that support numeric
casting as date/time values.

- mysqli:
. The return structure of mysqli_get_charset() no longer contains
the undocumented "comment" element. The value of "charsetnr" is
Expand Down
26 changes: 22 additions & 4 deletions ext/intl/common/common_date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,28 @@ U_CFUNC double intl_zval_to_millis(zval *z, intl_error *err)
}
}
} else {
/* TODO: try with cast(), get() to obtain a number */
intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
"invalid object type for date/time "
"(only IntlCalendar and DateTimeInterface permitted)");
zval casted;
ZVAL_UNDEF(&casted);

if (Z_OBJ_HT_P(z)->cast_object(Z_OBJ_P(z), &casted, _IS_NUMBER) == SUCCESS) {
if (EG(exception)) {
} else if (Z_TYPE(casted) == IS_LONG) {
rv = U_MILLIS_PER_SECOND * (double)Z_LVAL(casted);
} else if (Z_TYPE(casted) == IS_DOUBLE) {
rv = U_MILLIS_PER_SECOND * Z_DVAL(casted);
} else {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to gate it with !EG(exception)

Copy link
Copy Markdown
Contributor Author

@LamentXU123 LamentXU123 May 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah good catch :) Please check it again and see if now its right enough.

intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
"invalid object type for date/time "
"(IntlCalendar, DateTimeInterface, or numeric-castable object permitted)");
}
} else if (!EG(exception)) {
intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
"invalid object type for date/time "
"(IntlCalendar, DateTimeInterface, or numeric-castable object permitted)");
}
if (!Z_ISUNDEF(casted)) {
zval_ptr_dtor(&casted);
}
}
break;
case IS_REFERENCE:
Expand Down
24 changes: 24 additions & 0 deletions ext/intl/tests/dateformat_formatObject_numeric_object.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
IntlDateFormatter->format(): numeric-castable objects
--EXTENSIONS--
intl
zend_test
--INI--
intl.default_locale=en_US
date.timezone=UTC
--FILE--
<?php

$fmt = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'UTC', null, 'yyyy-MM-dd HH:mm:ss.SSS');
Comment thread
LamentXU123 marked this conversation as resolved.

echo $fmt->format(new NumericCastableNoOperations(0)), "\n";
echo datefmt_format($fmt, new NumericCastableNoOperations(0.5)), "\n";
var_dump($fmt->format(new stdClass()));
var_dump(intl_get_error_message());

?>
--EXPECT--
1970-01-01 00:00:00.000
1970-01-01 00:00:00.500
bool(false)
string(160) "IntlDateFormatter::format(): invalid object type for date/time (IntlCalendar, DateTimeInterface, or numeric-castable object permitted): U_ILLEGAL_ARGUMENT_ERROR"
4 changes: 2 additions & 2 deletions ext/intl/tests/dateformat_format_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ var_dump(intl_get_error_message());
?>
--EXPECT--
bool(false)
string(140) "IntlDateFormatter::format(): invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR"
string(160) "IntlDateFormatter::format(): invalid object type for date/time (IntlCalendar, DateTimeInterface, or numeric-castable object permitted): U_ILLEGAL_ARGUMENT_ERROR"
bool(false)
string(129) "datefmt_format(): invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR"
string(149) "datefmt_format(): invalid object type for date/time (IntlCalendar, DateTimeInterface, or numeric-castable object permitted): U_ILLEGAL_ARGUMENT_ERROR"
Loading