I make use of Codeigniters timezone_menu
which gives a drop down box of time timezones and I am wondering how are we supposed to make use of these timezones with PHPs date_default_timezone_set
?
An example of codeigniters timezone is UP1
. However, you can't use that with PHPs date_default_timezone_set
as the string it takes in should be something like Europe/Berlin
The question is, is there a way to convert a codeigniter timezone string to a PHP one that can be accepted by the date_default_timezone_set
?
You can get the UTC offset for one of the
U(M|P)x
-values by usingtimezones()
.From this point on, I think the only way is some sort of custom mapping between the offset and the PHP timezone. You could use
Etc/GMT(+|-)x
timezones but the problem with those will be that they do not contain any DST-transition information.The problem in general is, that the UTC-offsets actually map to a lot of different timezones, e.g.
UP1
((UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome) maps toEurope/Berlin
,Europe/Brussels
,Europe/Copenhagen
,Europe/Madrid
,Europe/Paris
,Europe/Rome
but also toAfrica/Luanda
for example. The thing now gets arbitrarily complex due to the DST-transitions that may oder may not occur in some of these timezones.If you don't need the DST-thing, a mapping between the return value of the
timezones()
function and theEtc/GMT(+|-)x
timezones should be OK, otherwise you cannot use the Codeigniter timezone handling at all because the mapping is not unambiguous.You can use the value of the timezone offset in the gmt_to_local() function for example:
Codeigniter gives the timezone reference back, which can be used with timezones() to get the time diff and from their you can calculate the time.
The following works for me:
EDIT The original function didn't exclude DST offsets. timezone_abbreviations_list() lists 2x for a timezone that has DST marked as true, for example Tasmania appears under +11 and +10. So if DST == TRUE, ignore it as part of the return listing.
Some examples: