Skip to content

datetime: new format "p", same as "P" but returning "Z" for UTC #5896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -8,6 +8,10 @@ PHP NEWS
. Fixed bug #79108 (Referencing argument in a function makes it a reference
in the stack trace). (Nikita)

- Date:
. Implemented FR #79903 (datetime: new format "p", same as "P" but returning
"Z" for UTC). (gharlan)

- JIT:
. Fixed bug #79864 (JIT segfault in Symfony OptionsResolver). (Dmitry)

Expand Down
6 changes: 6 additions & 0 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,12 @@ static zend_string *date_format(const char *format, size_t format_len, timelib_t

/* timezone */
case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
case 'p':
if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0) {
length = slprintf(buffer, sizeof(buffer), "%s", "Z");
break;
}
/* break intentionally missing */
case 'P': rfc_colon = 1; /* break intentionally missing */
case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
Expand Down
43 changes: 43 additions & 0 deletions ext/date/tests/date_format_timezone.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test date_format() function : timezone offset
--FILE--
<?php

$tz = array("UTC", "Europe/London", "Europe/Berlin", "America/Chicago");

foreach ($tz as $zone) {
echo $zone, "\n";
date_default_timezone_set($zone);

$date = date_create("2020-03-10 22:30:41");

var_dump( date_format($date, "O") );
var_dump( date_format($date, "P") );
var_dump( date_format($date, "p") );
}

echo "Z\n";
$date = date_create("2020-03-10 22:30:41Z");

var_dump( date_format($date, "p") );

?>
--EXPECT--
UTC
string(5) "+0000"
string(6) "+00:00"
string(1) "Z"
Europe/London
string(5) "+0000"
string(6) "+00:00"
string(6) "+00:00"
Europe/Berlin
string(5) "+0100"
string(6) "+01:00"
string(6) "+01:00"
America/Chicago
string(5) "-0500"
string(6) "-05:00"
string(6) "-05:00"
Z
string(1) "Z"