[php-src] Issue #20828: `DateTime::createFromFormat()` with the `!` format character does not set time zone to UTC
[email protected] (Sector6759)
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Issue: https://github.com/php/php-src/issues/20828
Author: Sector6759
### Description
The following code:
```php
<?php
declare(strict_types=1);
date_default_timezone_set('America/Chicago');
$dateTime = DateTime::createFromFormat('!Y-m-d', '2026-01-04');
var_dump($dateTime);
```
Resulted in this output:
```
class DateTime#1 (3) {
public $date =>
string(26) "2026-01-04 00:00:00.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(15) "America/Chicago"
}
```
But I expected this output instead:
```
class DateTime#1 (3) {
public $date =>
string(26) "2026-01-04 00:00:00.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(3) "UTC"
}
```
The [docs](https://www.php.net/manual/en/datetimeimmutable.createfromformat.php) say:
> All fields are initialised with the current date/time. In most cases you would want to reset these to "zero" (the Unix epoch, 1970-01-01 00:00:00 UTC). You do that by including the ! character as first character in your format, or | as your last. Please see the documentation for each character below for more information.
>
> ...
>
> | format character | Description | Example parsable values |
> | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
> | ! | Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values (`0` for hour, minute, second and fraction, `1` for month and day, `1970` for year and `UTC` for timezone information) | Without `!`, all fields will be set to the current date and time. |
In my code, I did not specify any timezone as part of the datetime parameter, but I included the `!` format character, so according to the docs, I expected the timezone to be set to `"UTC"` or `"+00:00"`.
### PHP Version
```plain
PHP 8.3.29 (cli) (built: Dec 18 2025 23:38:01) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.29, Copyright (c) Zend Technologies
with Zend OPcache v8.3.29, Copyright (c), by Zend Technologies
with Xdebug v3.5.0, Copyright (c) 2002-2025, by Derick Rethans
```
### Operating System
Ubuntu 24.04