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 UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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 date() and DateTimeInterface::format() when the
format prints no time zone offset, abbreviation or DST flag.

- DOM:
. Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for
small inputs.
Expand Down
96 changes: 64 additions & 32 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,45 @@ static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib
}
/* }}} */

/* The time zone offset of t, as the format characters I O P p T e Z c r print it. */
static timelib_time_offset *date_format_offset(const timelib_time *t)
{
timelib_time_offset *offset;

if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z + (t->dst * 3600));
offset->leap_secs = 0;
offset->is_dst = t->dst;
offset->abbr = timelib_strdup(t->tz_abbr);
} else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z);
offset->leap_secs = 0;
offset->is_dst = 0;
offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
snprintf(offset->abbr, 9, "GMT%c%02d%02d",
(offset->offset < 0) ? '-' : '+',
abs(offset->offset / 3600),
abs((offset->offset % 3600) / 60));
} else if (t->zone_type == TIMELIB_ZONETYPE_ID) {
offset = timelib_get_time_zone_info(t->sse, t->tz_info);
} else {
/* Shouldn't happen, but code defensively */
offset = timelib_time_offset_ctor();
}

return offset;
}

/* Sets *offset to the time zone offset of t the first time a local time needs it. */
static zend_always_inline void date_format_need_offset(timelib_time_offset **offset, const timelib_time *t, bool localtime)
{
if (localtime && !*offset) {
*offset = date_format_offset(t);
}
}

/* {{{ date_format - (gm)date helper */
static zend_string *date_format(const char *format, size_t format_len, const timelib_time *t, bool localtime)
{
Expand All @@ -686,31 +725,6 @@ static zend_string *date_format(const char *format, size_t format_len, const tim
return ZSTR_EMPTY_ALLOC();
}

if (localtime) {
if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z + (t->dst * 3600));
offset->leap_secs = 0;
offset->is_dst = t->dst;
offset->abbr = timelib_strdup(t->tz_abbr);
} else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z);
offset->leap_secs = 0;
offset->is_dst = 0;
offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
snprintf(offset->abbr, 9, "GMT%c%02d%02d",
(offset->offset < 0) ? '-' : '+',
abs(offset->offset / 3600),
abs((offset->offset % 3600) / 60));
} else if (t->zone_type == TIMELIB_ZONETYPE_ID) {
offset = timelib_get_time_zone_info(t->sse, t->tz_info);
} else {
/* Shouldn't happen, but code defensively */
offset = timelib_time_offset_ctor();
}
}

for (i = 0; i < format_len; i++) {
rfc_colon = false;
switch (format[i]) {
Expand Down Expand Up @@ -769,22 +783,31 @@ static zend_string *date_format(const char *format, size_t format_len, const tim
case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;

/* timezone */
case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
case 'I':
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0);
break;
case 'p':
date_format_need_offset(&offset, t, localtime);
if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0 || strcmp(offset->abbr, "GMT+0000") == 0) {
length = slprintf(buffer, sizeof(buffer), "%s", "Z");
break;
}
ZEND_FALLTHROUGH;
case 'P': rfc_colon = true; ZEND_FALLTHROUGH;
case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
case 'O':
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
localtime ? abs(offset->offset / 3600) : 0,
rfc_colon ? ":" : "",
localtime ? abs((offset->offset % 3600) / 60) : 0
);
break;
case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
case 'T':
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT");
break;
case 'e': if (!localtime) {
length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
} else {
Expand All @@ -793,9 +816,11 @@ static zend_string *date_format(const char *format, size_t format_len, const tim
length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
break;
case TIMELIB_ZONETYPE_ABBR:
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
break;
case TIMELIB_ZONETYPE_OFFSET: {
date_format_need_offset(&offset, t, localtime);
int seconds = offset->offset % 60;
if (seconds == 0) {
length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
Expand All @@ -816,18 +841,25 @@ static zend_string *date_format(const char *format, size_t format_len, const tim
}
}
break;
case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
case 'Z':
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0);
break;

/* full date/time */
case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
case 'c':
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
(zend_long) t->y, (int) t->m, (int) t->d,
(int) t->h, (int) t->i, (int) t->s,
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
localtime ? abs(offset->offset / 3600) : 0,
localtime ? abs((offset->offset % 3600) / 60) : 0
);
break;
case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
case 'r':
date_format_need_offset(&offset, t, localtime);
length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
php_date_short_day_name(t->y, t->m, t->d),
(int) t->d, mon_short_names[t->m - 1],
(zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
Expand All @@ -847,7 +879,7 @@ static zend_string *date_format(const char *format, size_t format_len, const tim

smart_str_0(&string);

if (localtime) {
if (offset) {
timelib_time_offset_dtor(offset);
}

Expand Down
43 changes: 43 additions & 0 deletions ext/date/tests/date_format_escaped_timezone_chars.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
date_format(): time zone characters, escaped and not, on every kind of zone
--INI--
date.timezone=UTC
--FILE--
<?php
$dates = [
new DateTimeImmutable('2026-07-01 12:00:00', new DateTimeZone('UTC')),
new DateTimeImmutable('2026-07-01 12:00:00', new DateTimeZone('Europe/Madrid')),
new DateTimeImmutable('2026-11-18T12:34:56-09:30'),
new DateTimeImmutable('2026-11-18 12:34:56 EST'),
];
foreach ($dates as $d) {
var_dump($d->format('\I\O\P\p\T\e\Z\c\r Y-m-d\TH:i:s'));
var_dump($d->format('\\\\T \TT'));
var_dump($d->format('e p T P O Z I'));
var_dump($d->format('Y-m-d H:i:s T (P) e'));
var_dump($d->format('c | r'));
}
var_dump(gmdate('e p T P O Z I c', 1763469296));
?>
--EXPECT--
string(29) "IOPpTeZcr 2026-07-01T12:00:00"
string(9) "\UTC TUTC"
string(26) "UTC Z UTC +00:00 +0000 0 0"
string(36) "2026-07-01 12:00:00 UTC (+00:00) UTC"
string(59) "2026-07-01T12:00:00+00:00 | Wed, 01 Jul 2026 12:00:00 +0000"
string(29) "IOPpTeZcr 2026-07-01T12:00:00"
string(11) "\CEST TCEST"
string(45) "Europe/Madrid +02:00 CEST +02:00 +0200 7200 1"
string(47) "2026-07-01 12:00:00 CEST (+02:00) Europe/Madrid"
string(59) "2026-07-01T12:00:00+02:00 | Wed, 01 Jul 2026 12:00:00 +0200"
string(29) "IOPpTeZcr 2026-11-18T12:34:56"
string(19) "\GMT-0930 TGMT-0930"
string(44) "-09:30 -09:30 GMT-0930 -09:30 -0930 -34200 0"
string(44) "2026-11-18 12:34:56 GMT-0930 (-09:30) -09:30"
string(59) "2026-11-18T12:34:56-09:30 | Wed, 18 Nov 2026 12:34:56 -0930"
string(29) "IOPpTeZcr 2026-11-18T12:34:56"
string(9) "\EST TEST"
string(36) "EST -05:00 EST -05:00 -0500 -18000 0"
string(36) "2026-11-18 12:34:56 EST (-05:00) EST"
string(59) "2026-11-18T12:34:56-05:00 | Wed, 18 Nov 2026 12:34:56 -0500"
string(52) "UTC Z GMT +00:00 +0000 0 0 2025-11-18T12:34:56+00:00"
Loading