Codeigniter, timezone_menu and date_default_timezone_set

I am using Codeigniters timezone_menu , which gives a drop down list of time zones, and I wonder how we should use these time zones with PHP date_default_timezone_set ?

An example timezone for codeigniters is UP1 . However, you cannot use this with PHP date_default_timezone_set , since the line it accepts in should be something like Europe/Berlin

The question is, is there a way to convert the codeigniter timezone string to PHP, which can be accepted by date_default_timezone_set ?

+7
source share
4 answers

The following works for me:

 // Example - timezones is part of the date helper function // You obviously wouldn't echo it out, you would pass it in to // set default timezone function in PHP based on the posted data from // the dropdown echo timezone_by_offset(timezones('UM5')); // ouputs 'America/Porto_Acre' which is (-5) // And the function function timezone_by_offset($offset) { $offset = ($offset+1) * 60 * 60; $abbrarray = timezone_abbreviations_list(); foreach ($abbrarray as $abbr) { foreach ($abbr as $city) { if ($city['offset'] == $offset) { echo($city['timezone_id']); return true; } } } echo "UTC"; return false; } 

EDIT The original function did not exclude a DST offset. timezone_abbreviations_list () displays 2x for a time zone that has DST marked as true, for example, Tasmania is displayed under +11 and +10. Therefore, if DST == TRUE, ignore it as part of the returned list.

 //-- Function ------------------------------------------------------------------- function timezone_by_offset($offset) { $abbrarray = timezone_abbreviations_list(); $offset = $offset * 60 * 60; foreach ($abbrarray as $abbr) { foreach ($abbr as $city) { if ($city['offset'] == $offset && $city['dst'] == FALSE) { return $city['timezone_id']; } } } return 'UTC'; // any default value you wish } 

Some examples:

 //-- Test Cases ------------------------------------------------------------------- echo timezone_by_offset(-12) . '<br/>'; echo (date_default_timezone_set(timezone_by_offset(-12)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; // Etc/GMT+12 // Valid echo timezone_by_offset(-10) . '<br/>'; echo (date_default_timezone_set(timezone_by_offset(-10)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; // America/Anchorage // Valid echo timezone_by_offset(-8) . '<br/>'; echo (date_default_timezone_set(timezone_by_offset(-8)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; // Etc/GMT+8 // Valid echo timezone_by_offset(6) . '<br/>'; echo (date_default_timezone_set(timezone_by_offset(6)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; // Asia/Aqtobe // Valid echo timezone_by_offset(7) . '<br/>'; echo (date_default_timezone_set(timezone_by_offset(7)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; // Indian/Christmas // Valid 
+7
source

You can get the UTC offset for one of the values ​​of U(M|P)x using timezones() .

 $offset = timezones('UP1'); // don't know the exact format returned but should be something like 1 or '+01:00' 

From now on, I think the only way is some kind of custom mapping between the offset and the PHP timezone. You can use the time zones Etc/GMT(+|-)x , but the problem with them will be that they do not contain any information about the DST transition.

The problem in general is that UTC offsets are actually mapped to many different time zones, for example. UP1 ((UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome) is displayed on Europe/Berlin , Europe/Brussels , Europe/Copenhagen , Europe/Madrid , Europe/Paris , Europe/Rome , but also Africa/Luanda for example. Now the thing is becoming arbitrarily complex due to DST transitions that may not be available in some of these time zones.

If you don’t need the DST thing, the comparison between the return value of the timezones() function and the time zones Etc/GMT(+|-)x should be OK, otherwise you cannot use Codeigniter time zone processing at all, since the display is not unique.

+2
source

Codeigniter returns a time zone link that can be used with timezones () to get the time difference, and from them you can calculate the time.

0
source

You can use the time zone offset value in the gmt_to_local () function, for example:

 //set the time offset based on the users timezone settings $timezone = 'UM7'; $offset = timezones($timezone); $servertime = time(); $mytime = gmt_to_local($servertime,$offset); $time = date("M d, Y h:i A", $mytime); //example result //Dec 22, 2011 04:18 PM 
0
source

All Articles