Jump to content

Brian_15

New Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by Brian_15

  1. Need help with modifying current code to work with en_US instead of en-US and so on.

    <?php
    	// Languages we support
    	// $available_languages = array("en", "en-us"); # old
            $available_languages = array("en", "en_US"); # new
    	$default_language = "en"; // a default language to fall back to in case there's no match
    	function prefered_language($available_languages, $http_accept_language) {
    		global $default_language;
    		$available_languages = array_flip($available_languages);
    		$langs = array();
    		preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', $http_accept_language, $matches, PREG_SET_ORDER);
    		foreach($matches as $match) {
    			list($a, $b) = explode('-', $match[1]) + array('', '');
    			$value = isset($match[2]) ? (float) $match[2] : 1.0;
    			if(isset($available_languages[$match[1]])) {
    				$langs[$match[1]] = $value;
    				continue;
    			}
    			if(isset($available_languages[$a])) {
    				$langs[$a] = $value - 0.1;
    			}
    		}
    		if($langs) {
    			arsort($langs);
    			return key($langs); // We don't need the whole array of choices since we have a match
    		} else {
    			return $default_language;
    		}
    	}
    	$lang = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
    	print "<h3>Site available in:</h3><pre>";
    	print_r($available_languages);
    	print "</pre>\n<h3>Browser supported languages:</h3><pre>";
    	print_r(explode(',',  $_SERVER["HTTP_ACCEPT_LANGUAGE"]));
    	print "</pre>\n<h3>site will display in: <em>".$lang."</em></h3>";
    ?>
    
  2. 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>
                             ...................................
    
  3. 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>
    
  4. 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>
    
×
×
  • 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.