Skip to content

DateTimeZone fails to parse time zones that contain the "+" character #10218

Closed

Description

@spideyfusion

Description

The following code:

<?php
var_dump(new DateTime('now', new DateTimeZone('Etc/GMT+1')));

Resulted in this output:

Fatal error: Uncaught Exception: DateTimeZone::__construct(): Unknown or bad timezone (Etc/GMT+1) in /in/bGhje:3
Stack trace:
#0 /in/bGhje(3): DateTimeZone->__construct('Etc/GMT+1')
#1 {main}
  thrown in /in/bGhje on line 3

But I expected this output instead:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2023-01-04 12:37:42.004396"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(9) "Etc/GMT+1"
}

3v4l.org link: https://3v4l.org/bGhje#v8.2.1


Looks like it broke after the latest timelib update. Looking through the recent changes, it seems that the + character is not considered in this lookup function.

@derickr Could you take a look at this?

PHP Version

PHP 8.2.1 RC1

Operating System

No response

Activity

  1. heiglandreas commented on Jan 4, 2023

    @heiglandreas
    Contributor

    As per https://www.php.net/manual/en/timezones.others.php this can be expected behaviour as you should not use those! Depending on the implemented timezone-db this can break.

  2. cmb69 commented on Jan 4, 2023

    @cmb69
    Member

    This behavioral change has likely been introduced by fixing #9700.

  3. hormus commented on Jan 5, 2023

    @hormus

    i'm in favor of reverting to previous code, user side it's easy to interpret even if you use convert it's good for 3rd party code

    <?php
    
    $date = date_create();
    $date = date_create('1970-01-01 00:00:00 +' . date_format($date, 'U') . 'sec', timezone_open('UTC'));
    $usercode_tz = timezone_open('Etc/GMT+1'); // UTC-1
    date_timezone_set($date, $usercode_tz);
    $date_display = date_format($date, 'Y-m-d\\TH:i:s e');
    $user_display = str_replace(' ', ' [', $date_display) . ']';
    $convert_user_display = str_replace(array(' [', ']'), '', $user_display);
    var_dump($convert_user_display);
    ?>
  4. kalenjohnson commented on Jan 6, 2023

    @kalenjohnson

    Looks like this also affects 8.1.14

  5. xZero707 commented on Jan 10, 2023

    @xZero707

    Can confirm this issue affects >= 8.1.14 and is not present in versions bellow.
    Running code from first example:

    Docker - 8.1.13

    /app/test.php:2:
    class DateTime#1 (3) {
      public $date =>
      string(26) "2023-01-10 08:15:09.977455"
      public $timezone_type =>
      int(3)
      public $timezone =>
      string(9) "Etc/GMT+1"
    }
    

    Docker - 8.1.14

    Fatal error: Uncaught Exception: DateTimeZone::__construct(): Unknown or bad timezone (Etc/GMT+1) in /app/test.php:2
    Stack trace:
    #0 /app/test.php(2): DateTimeZone->__construct('Etc/GMT+1')
    #1 {main}
      thrown in /app/test.php on line 2
    
  6. added a commit that references this issue on Jan 10, 2023
    d12ba11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions