Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP timezone Etc/GMT+5 becomes -0500?

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?

like image 265
mauris Avatar asked Apr 26 '14 06:04

mauris


2 Answers

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.g Etc/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));.

like image 186
Amal Murali Avatar answered Oct 13 '22 01:10

Amal Murali


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

like image 28
noorwala Avatar answered Oct 12 '22 23:10

noorwala