Brian_15 Posted July 26, 2016 Share Posted July 26, 2016 How do improve this class to output offset like + 02:00, - 02:00 What i want to achieve is this output: [ UTC/GMT - 11:00 ] - Timezone name class Time_zone { private $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Artic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); public function generate_list() { $time_zones = array(); foreach ($this->regions as $name => $mask) { $time_zones[$name] = DateTimeZone::listIdentifiers($mask); } return $time_zones; } } $tz = new Time_zone(); ?> <pre><?=print_r($tz->generate_list());?></pre> Quote Link to comment https://forums.phpfreaks.com/topic/301638-add-date-offset-to-current-code/ Share on other sites More sharing options...
requinix Posted July 26, 2016 Share Posted July 26, 2016 Your generate_list() will return an array of arrays. Loop over those to get timezone identifiers, construct a DateTimeZone for each, then use its getOffset() method with some quick math (to convert seconds to hours+minutes) to form that output. Quote Link to comment https://forums.phpfreaks.com/topic/301638-add-date-offset-to-current-code/#findComment-1535077 Share on other sites More sharing options...
Solution Barand Posted July 26, 2016 Solution Share Posted July 26, 2016 Create a dateTime object for each timezone. Format the time with 'P' format. class Time_zone { private $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Artic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); public function generate_list() { $time_zones = array(); foreach ($this->regions as $name => $mask) { $time_zones[$name] = DateTimeZone::listIdentifiers($mask); } foreach ($time_zones as &$zones) { foreach ($zones as &$zstr) { $zn = new DateTimeZone($zstr); $t = new DateTime('now', $zn); $offset = $t->format('P'); $zstr = "[UTC/GMT $offset] $zstr"; } } return $time_zones; } } Quote Link to comment https://forums.phpfreaks.com/topic/301638-add-date-offset-to-current-code/#findComment-1535080 Share on other sites More sharing options...
Brian_15 Posted July 26, 2016 Author Share Posted July 26, 2016 (edited) Testing code $time_zone = new Time_zone(); ?> <select> <?php foreach($time_zone->generate_list() as $region => $list) { ?> <optgroup label="<?=$region?>"> <?php foreach($list as $name) { ?> <option value="<?=$name?>"><?=$name?></option> <?php } ?> </optgroup> <?php } ?> </select> Current output <option value="[UTC/GMT +00:00] Africa/Abidjan">[UTC/GMT +00:00] Africa/Abidjan</option> Expected output <option value="Africa/Abidjan">[UTC/GMT +00:00] Africa/Abidjan</option> Edited July 26, 2016 by Brian_15 Quote Link to comment https://forums.phpfreaks.com/topic/301638-add-date-offset-to-current-code/#findComment-1535083 Share on other sites More sharing options...
Barand Posted July 26, 2016 Share Posted July 26, 2016 How do improve this class to output offset like + 02:00, - 02:00 What i want to achieve is this output: [ UTC/GMT - 11:00 ] - Timezone name What you are now asking for is not what you originally asked for. I am not getting involved in an ever-escalating spiral of requirements (Not for free anyway). Hint: make "Africa/Abidjan" the key of the returned array and "[uTC/GMT +00:00] Africa/Abidjan" the value then build your options using both. Quote Link to comment https://forums.phpfreaks.com/topic/301638-add-date-offset-to-current-code/#findComment-1535090 Share on other sites More sharing options...
Brian_15 Posted July 28, 2016 Author Share Posted July 28, 2016 (edited) Now its woking <?php class Time_zone { protected $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Artic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); public function get_list() { $time_zones = array(); foreach ($this->regions as $name => $mask) { foreach(DateTimeZone::listIdentifiers($mask) as $time_zone) { $time_zones[$name][$time_zone] = '[UTC/GMT ' . $this->get_offset($time_zone) . '] ' . $time_zone; } } return $time_zones; } protected function get_offset($time_zone) { $_tz = new DateTimeZone($time_zone); $_dt = new DateTime('now', $_tz); return $_dt->format('P'); } } <?php $time_zone = new Time_zone(); ?> <select> <?php foreach($time_zone->get_list() as $region => $list) { ?> <optgroup label="<?=$region?>"> <?php foreach($list as $time_zone => $name) { ?> <option value="<?=$time_zone?>"><?=$name?></option> <?php } ?> </optgroup> <?php } ?> </select> Output <select> <optgroup label="Africa"> <option value="Africa/Abidjan">[UTC/GMT +00:00] Africa/Abidjan</option> <option value="Africa/Accra">[UTC/GMT +00:00] Africa/Accra</option> ................................... </optgroup> <optgroup label="America"> <option value="America/Adak">[UTC/GMT -09:00] America/Adak</option> <option value="America/Anchorage">[UTC/GMT -08:00] America/Anchorage</option> ................................... Edited July 28, 2016 by Brian_15 Quote Link to comment https://forums.phpfreaks.com/topic/301638-add-date-offset-to-current-code/#findComment-1535232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.