Skip to content
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

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

Closed
spideyfusion opened this issue Jan 4, 2023 · 5 comments
Closed

Comments

@spideyfusion
Copy link
Contributor

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

@heiglandreas
Copy link
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.

@cmb69
Copy link
Member

cmb69 commented Jan 4, 2023

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

@hormus
Copy link

hormus commented Jan 5, 2023

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);
?>

@kalenjohnson
Copy link

Looks like this also affects 8.1.14

@xZero707
Copy link

xZero707 commented Jan 10, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants
@heiglandreas @derickr @spideyfusion @kalenjohnson @cmb69 @xZero707 @hormus and others