Jump to content

Add date offset to current code


Brian_15
Go to solution Solved by Barand,

Recommended Posts

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>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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;

    }
    
}

Link to comment
Share on other sites

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 by Brian_15
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Brian_15
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.