Skip to content

Commit a6e3ce4

Browse files
gharlancmb69
authored andcommitted
datetime: new format "p", same as "P" but returning "Z" for UTC
1 parent bb8b95b commit a6e3ce4

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
- Date:
1414
. Fixed bug #60302 (DateTime::createFromFormat should new static(), not new
1515
self()). (Derick)
16+
. Implemented FR #79903 (datetime: new format "p", same as "P" but returning
17+
"Z" for UTC). (gharlan)
1618

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

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ PHP 8.0 UPGRADE NOTES
683683
- Date:
684684
. Added DateTime::createFromInterface() and
685685
DateTimeImmutable::createFromInterface().
686+
. Added the DateTime format specifier "p" which is the same as "P" but
687+
returning "Z" for UTC.
686688

687689
- Dom:
688690
. Introduce DOMParentNode and DOMChildNode with new traversal and

ext/date/php_date.c

+6
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,12 @@ static zend_string *date_format(const char *format, size_t format_len, timelib_t
711711

712712
/* timezone */
713713
case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
714+
case 'p':
715+
if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0) {
716+
length = slprintf(buffer, sizeof(buffer), "%s", "Z");
717+
break;
718+
}
719+
/* break intentionally missing */
714720
case 'P': rfc_colon = 1; /* break intentionally missing */
715721
case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
716722
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Test date_format() function : timezone offset
3+
--FILE--
4+
<?php
5+
6+
$tz = array("UTC", "Europe/London", "Europe/Berlin", "America/Chicago");
7+
8+
foreach ($tz as $zone) {
9+
echo $zone, "\n";
10+
date_default_timezone_set($zone);
11+
12+
$date = date_create("2020-03-10 22:30:41");
13+
14+
var_dump( date_format($date, "O") );
15+
var_dump( date_format($date, "P") );
16+
var_dump( date_format($date, "p") );
17+
}
18+
19+
echo "Z\n";
20+
$date = date_create("2020-03-10 22:30:41Z");
21+
22+
var_dump( date_format($date, "p") );
23+
24+
?>
25+
--EXPECT--
26+
UTC
27+
string(5) "+0000"
28+
string(6) "+00:00"
29+
string(1) "Z"
30+
Europe/London
31+
string(5) "+0000"
32+
string(6) "+00:00"
33+
string(6) "+00:00"
34+
Europe/Berlin
35+
string(5) "+0100"
36+
string(6) "+01:00"
37+
string(6) "+01:00"
38+
America/Chicago
39+
string(5) "-0500"
40+
string(6) "-05:00"
41+
string(6) "-05:00"
42+
Z
43+
string(1) "Z"

0 commit comments

Comments
 (0)