CodeIgniter Forums
Restricted Timezones - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Restricted Timezones (/thread-3989.html)



Restricted Timezones - El Forum - 11-01-2007

[eluser]Unknown[/eluser]
I need to offer a set of restriced timezones (just -8 -> -3.5) for Canadians.

I've modified date_helper.php to accomodate this:

Code:
function timezone_menu($default = 'UTC', $class = "", $name = 'timezones',$options = array('UM25','UM4','UM5','UM6','UM7','UM8'))
{
    $CI =& get_instance();
    $CI->lang->load('date');
    
    if ($default == 'GMT')
        $default = 'UTC';

    $menu = '<select name="'.$name.'"';
    
    if ($class != '')
    {
        $menu .= ' class="'.$class.'"';
    }
    
    $menu .= ">\n";
    
    foreach (timezones() as $key => $val)
    {
      if(in_array($key,$options)) {
          $selected = ($default == $key) ? " selected='selected'" : '';
          $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
      }
    }

    $menu .= "</select>";

    return $menu;
}

Jee


Restricted Timezones - El Forum - 11-02-2007

[eluser]xwero[/eluser]
It's a nasty looking hack because you hard coded your options
Code:
function timezone_menu($default = 'UTC', $class = "", $name = 'timezones',$limittz = '')
{
    $CI =& get_instance();
    $CI->lang->load('date');
    
    if ($default == 'GMT')
        $default = 'UTC';

    $menu = '<select name="'.$name.'"';
    
    if ($class != '')
    {
        $menu .= ' class="'.$class.'"';
    }
    
    $menu .= ">\n";
    
        $timezones = (is_array($limittz)?timezones($limittz):timezones();

    foreach ($timezones as $key => $val)
    {
        $selected = ($default == $key) ? " selected='selected'" : '';
        $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
    }

    $menu .= "</select>";

    return $menu;
}
    
// ------------------------------------------------------------------------

/**
* Timezones
*
* Returns an array of timezones.  This is a helper function
* for various other ones in this library
*
* @access    public
* @param    string    timezone
* @return    string
*/    
function timezones($tz = '')
{
    // Note: Don't change the order of these even though
    // some items appear to be in the wrong order
        
    $zones = array(
                    'UM12' => -12,
                    'UM11' => -11,
                    'UM10' => -10,
                    'UM9'  => -9,
                    'UM8'  => -8,
                    'UM7'  => -7,
                    'UM6'  => -6,
                    'UM5'  => -5,
                    'UM4'  => -4,
                    'UM25' => -2.5,
                    'UM3'  => -3,
                    'UM2'  => -2,
                    'UM1'  => -1,
                    'UTC'  => 0,
                    'UP1'  => +1,
                    'UP2'  => +2,
                    'UP3'  => +3,
                    'UP25' => +2.5,
                    'UP4'  => +4,
                    'UP35' => +3.5,
                    'UP5'  => +5,
                    'UP45' => +4.5,
                    'UP6'  => +6,
                    'UP7'  => +7,
                    'UP8'  => +8,
                    'UP9'  => +9,
                    'UP85' => +8.5,
                    'UP10' => +10,
                    'UP11' => +11,
                    'UP12' => +12
                );
                
    if ($tz == '' && !is_array($tz))
    {
        return $zones;
    }
        
        if(is_array($tz))
        {
           $return = array();
           foreach($tz as $t)
           {
              if(isset($zones[$t])){ $return[$t] = $zones[$t]; }
           }
           return $return;
        }
    
        if($tz != '')
        {
      if ($tz == 'GMT'){ $tz = 'UTC'; }
          return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
        }
}

Now you have two enchantments instead of one

- limit the timezones of the timezone menu
- get a limited, the full timezones array or just one timezone from the timezones function