As with many web apps, the app I'm developing needs to work with converting timezones for our dear end users. However, as I worked with Carbon / DateTime in PHP 5.3, I found this weird:
$date_str = '2014-04-15 12:00:00'; // from database
$date = new DateTime($date_str,new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone('Etc/GMT+5'));
echo $date->format('Y-m-d H:i:s O'); // 2014-04-15 07:00:00 -0500
I set the $date
's timezone to GMT+5, but when I format the $date
, it becomes -0500
instead of the expected +0500
.
Likewise if I set it to negative, i.e. Etc/GMT-5
, the format shows +0500
instead of the expected -0500
.
Anyone able to enlighten?
I was able to replicate this problem in PHP 5.3.3, 5.3.13, 5.3.28. Thought it was a PHP-specific-version bug. A bug or feature?
This is an interesting case. The Etc/GMT
timezones have their sign reversed.
From the Wikipedia article on tz database:
In order to conform with the POSIX style, those zone names beginning with
Etc/GMT
have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign in their name (e.gEtc/GMT-14
is 14 hours ahead/east of GMT.)
The PHP manual also warns you not to use this timezone:
Warning: Please do not use any of the timezones listed here (besides UTC), they only exist for backward compatible reasons.
Instead of using an offset, use a specific timezone identifier. To get a list of valid timezones, you can use print_r(DateTimeZone::listIdentifiers(DateTimeZone::ALL));
.
i hope it help you
just remove
etc/gmt
with gtm+offset
$date_str = '2014-04-15 12:00:00'; // from database
$date = new DateTime($date_str,new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone('GMT+5'));
echo $date->format('Y-m-d H:i:s O');
Output 2014-04-15 17:00:00 +0500
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With