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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ PHP NEWS
passing an array of CURLFile or CURLStringFile objects to
CURLOPT_POSTFIELDS. (Sjoerd Langkemper)

- Date:
. Improved performance of new DateTime(), new DateTimeImmutable() and
date_create() without a time string or with "now". (Alexander Danilov)

- FPM:
. Use recommended TYPE, HELP order for OpenMetrics metadata.
(Marcel Hernandez)
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ PHP 8.7 UPGRADE NOTES
. Improved performance of integer to string conversion (string casts, echo,
implode(), json_encode(), serialize(), var_export() and others).

- Date:
. Improved performance of new DateTime(), new DateTimeImmutable() and
date_create() without a time string or with "now".

- DOM:
. Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for
small inputs.
Expand Down
14 changes: 8 additions & 6 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2403,9 +2403,11 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
time_t sec;
suseconds_t usec;
int options = 0;
bool is_now = false;

if (dateobj->time) {
timelib_time_dtor(dateobj->time);
dateobj->time = NULL;
}
if (format) {
if (time_str_len == 0) {
Expand All @@ -2417,7 +2419,10 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
time_str = "now";
time_str_len = sizeof("now") - 1;
}
dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
is_now = time_str_len == sizeof("now") - 1 && memcmp(time_str, ZEND_STRL("now")) == 0;
if (!is_now) {
dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
}
}

/* update last errors and warnings */
Expand Down Expand Up @@ -2456,7 +2461,7 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
return 0;
}
type = tzobj->type;
} else if (dateobj->time->tz_info) {
} else if (dateobj->time && dateobj->time->tz_info) {
tzi = dateobj->time->tz_info;
} else {
tzi = get_timezone_info();
Expand Down Expand Up @@ -2484,10 +2489,7 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
timelib_unixtime2local(now, (timelib_sll) sec);
php_date_set_time_fraction(now, usec);

if (!format
&& time_str_len == sizeof("now") - 1
&& memcmp(time_str, "now", sizeof("now") - 1) == 0) {
timelib_time_dtor(dateobj->time);
if (is_now) {
dateobj->time = now;
return 1;
}
Expand Down
12 changes: 12 additions & 0 deletions ext/date/tests/DateTime_construct_basic1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ var_dump( new DateTime("GMT") );
var_dump( new DateTime("2005-07-14 22:30:41") );
var_dump( new DateTime("2005-07-14 22:30:41 GMT") );

echo "\n-- Calling the constructor again resets the time zone --\n";
$d = new DateTime("2005-07-14 22:30:41 Asia/Tokyo");
$d->__construct();
var_dump($d->getTimezone()->getName());
$d = new DateTime("2005-07-14 22:30:41 Asia/Tokyo");
$d->__construct("now");
var_dump($d->getTimezone()->getName());

?>
--EXPECTF--
*** Testing new DateTime() : basic functionality ***
Expand Down Expand Up @@ -48,3 +56,7 @@ object(DateTime)#%d (3) {
["timezone"]=>
string(3) "GMT"
}

-- Calling the constructor again resets the time zone --
string(13) "Europe/London"
string(13) "Europe/London"
31 changes: 31 additions & 0 deletions ext/date/tests/date_get_last_errors_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@ date.timezone=UTC
--FILE--
<?php
var_dump(date_get_last_errors()); // no date was parsed, so no errors

// Constructing "now" must reset errors left over from a failed parse.
foreach ([
'new DateTime()' => fn() => new DateTime(),
'new DateTime("now")' => fn() => new DateTime('now'),
'new DateTimeImmutable("now")' => fn() => new DateTimeImmutable('now'),
'date_create()' => fn() => date_create(),
'date_create("now")' => fn() => date_create('now'),
] as $name => $create) {
date_create('garbage');
var_dump(date_get_last_errors()['error_count'] > 0);
$create();
echo $name, ': ';
var_dump(date_get_last_errors());
}

// A failed parse after a successful "now" must still record errors.
new DateTime();
date_create('garbage');
var_dump(date_get_last_errors()['error_count'] > 0);
?>
--EXPECT--
bool(false)
bool(true)
new DateTime(): bool(false)
bool(true)
new DateTime("now"): bool(false)
bool(true)
new DateTimeImmutable("now"): bool(false)
bool(true)
date_create(): bool(false)
bool(true)
date_create("now"): bool(false)
bool(true)
Loading