EllisLab text mark
Advanced Search
     
Restricted Timezones
Posted: 01 November 2007 06:57 AM   [ Ignore ]
Joined: 2007-10-30
1 posts

I need to offer a set of restriced timezones (just -8 -> -3.5) for Canadians.

I’ve modified date_helper.php to accomodate this:

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

 
Posted: 02 November 2007 05:47 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

It’s a nasty looking hack because you hard coded your options

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])) ? $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