Convert UTC offset to timezone or date
It can be done quite simply, by turning the offset into seconds and passing it to timezone_name_from_abbr
:
<?php$offset = '-7:00';// Calculate seconds from offsetlist($hours, $minutes) = explode(':', $offset);$seconds = $hours * 60 * 60 + $minutes * 60;// Get timezone name from seconds$tz = timezone_name_from_abbr('', $seconds, 1);// Workaround for bug #44780if($tz === false) $tz = timezone_name_from_abbr('', $seconds, 0);// Set timezonedate_default_timezone_set($tz);echo $tz . ': ' . date('r');
The third parameter of timezone_name_from_abbr
controls whether to adjust for daylight saving time or not.
timezone_name_from_abbr() will return false on some time zone offsets. In particular - Hawaii, which has a -10 from GMT offset, -36000 seconds.
References:
date_default_timezone_set('UTC');$timezones = array();foreach (DateTimeZone::listAbbreviations() as $key => $array){ $timezones = array_merge($timezones, $array);}$utc = new DateTimeZone('UTC');$timezone_offset = '+02:00'; # 2H$sign = substr($timezone_offset, 0, 1) == '+'? '': '-';$offset = substr($timezone_offset, 1, 2) . 'H' . substr($timezone_offset, 4, 2) . 'M';$operation = $sign == ''? 'add': 'sub';$start = new DateTime('', $utc);$date = new DateTime('', $utc);$date->{$operation}(new DateInterval("PT{$offset}"));$offset = $start->diff($date)->format('%r') . ($start->diff($date)->h * 3600 + $start->diff($date)->m * 60 + $start->diff($date)->s); # 7200 (2H)echo $offset, PHP_EOL;echo $date->format('Y-m-d H:i:s'), PHP_EOL;foreach($timezones as $timezone){ if($timezone['offset'] == $offset) { echo $timezone['timezone_id'], PHP_EOL; }}
I might have misunderstood you in some parts, but I hope it helps, if you could be more specific, I might be more helpful.
For Chile I get:
-25200 (-7h)2012-08-07 18:05:24 (current time 2012-08-08 01:05:24)Chile/EasterIsland
Output of the example above:
72002012-08-08 02:49:56Europe/LondonEurope/BelfastEurope/GibraltarEurope/GuernseyEurope/Isle_of_ManEurope/JerseyGBAfrica/KhartoumAfrica/BlantyreAfrica/BujumburaAfrica/GaboroneAfrica/HarareAfrica/KigaliAfrica/LubumbashiAfrica/LusakaAfrica/MaputoAfrica/WindhoekEurope/BerlinAfrica/AlgiersAfrica/CeutaAfrica/TripoliAfrica/TunisArctic/LongyearbyenAtlantic/Jan_MayenCETEurope/AmsterdamEurope/AndorraEurope/AthensEurope/BelgradeEurope/BratislavaEurope/BrusselsEurope/BudapestEurope/ChisinauEurope/CopenhagenEurope/GibraltarEurope/KaliningradEurope/KievEurope/LisbonEurope/LjubljanaEurope/LuxembourgEurope/MadridEurope/MaltaEurope/MinskEurope/MonacoEurope/OsloEurope/ParisEurope/PodgoricaEurope/PragueEurope/RigaEurope/RomeEurope/San_MarinoEurope/SarajevoEurope/SimferopolEurope/SkopjeEurope/SofiaEurope/StockholmEurope/TallinnEurope/TiraneEurope/TiraspolEurope/UzhgorodEurope/VaduzEurope/VaticanEurope/ViennaEurope/VilniusEurope/WarsawEurope/ZagrebEurope/ZaporozhyeEurope/ZurichWETEurope/KaliningradEurope/HelsinkiAfrica/CairoAfrica/TripoliAsia/AmmanAsia/BeirutAsia/DamascusAsia/GazaAsia/IstanbulAsia/NicosiaEETEurope/AthensEurope/BucharestEurope/ChisinauEurope/IstanbulEurope/KaliningradEurope/KievEurope/MariehamnEurope/MinskEurope/MoscowEurope/NicosiaEurope/RigaEurope/SimferopolEurope/SofiaEurope/TallinnEurope/TiraspolEurope/UzhgorodEurope/VilniusEurope/WarsawEurope/ZaporozhyeAsia/JerusalemAsia/GazaAsia/Tel_AvivMETAfrica/JohannesburgAfrica/MaseruAfrica/MbabaneAfrica/WindhoekAfrica/WindhoekAfrica/NdjamenaEurope/LisbonEurope/MadridEurope/MonacoEurope/ParisWETEurope/Luxembourg
Which nails my timezone.
$UTC_offset = '+03:00';$date = new \DateTime('now', 'UTC');var_dump($date);$timezone = new \DateTimeZone(str_replace(':', '', $UTC_offset));$date->setTimezone($timezone);var_dump($date);
Results:
class DateTime#205 (3) { public $date => string(26) "2015-01-20 06:00:00.000000" public $timezone_type => int(3) public $timezone => string(3) "UTC"}class DateTime#205 (3) { public $date => string(26) "2015-01-20 09:00:00.000000" public $timezone_type => int(1) public $timezone => string(6) "+03:00"}