Advertisement
Guest User

Untitled

a guest
Apr 1st, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2.  
  3. App::uses('AppHelper','View/Helper');
  4.  
  5. class TimeZoneHelper extends AppHelper {
  6.      
  7.     public $helpers = array('Form');
  8.    
  9.     function generateList() {
  10.    
  11.         $list = DateTimeZone::listAbbreviations();
  12.         $idents = DateTimeZone::listIdentifiers();
  13.    
  14.         $data = $offset = $added = array();
  15.         foreach ($list as $abbr => $info) {
  16.             foreach ($info as $zone) {
  17.                 if ( ! empty($zone['timezone_id'])
  18.                     AND
  19.                     ! in_array($zone['timezone_id'], $added)
  20.                     AND
  21.                       in_array($zone['timezone_id'], $idents)) {
  22.                     $z = new DateTimeZone($zone['timezone_id']);
  23.                     $c = new DateTime(null, $z);
  24.                     $zone['time'] = $c->format('H:i a');
  25.                     $data[] = $zone;
  26.                     $offset[] = $z->getOffset($c);
  27.                     $added[] = $zone['timezone_id'];
  28.                 }
  29.             }
  30.         }
  31.    
  32.         array_multisort($offset, SORT_ASC, $data);
  33.         $options = array();
  34.         foreach ($data as $key => $row) {
  35.             $options[$row['timezone_id']] = $row['time'] . ' - '
  36.                                             . $this->formatOffset($row['offset'])
  37.                                             . ' ' . $row['timezone_id'];
  38.         }
  39.        
  40.         return $options;
  41.  
  42.     }
  43.  
  44.     function formatOffset($offset) {
  45.             $hours = $offset / 3600;
  46.             $remainder = $offset % 3600;
  47.             $sign = $hours > 0 ? '+' : '-';
  48.             $hour = (int) abs($hours);
  49.             $minutes = (int) abs($remainder / 60);
  50.    
  51.             if ($hour == 0 AND $minutes == 0) {
  52.                 $sign = ' ';
  53.             }
  54.             return 'GMT' . $sign . str_pad($hour, 2, '0', STR_PAD_LEFT)
  55.                     .':'. str_pad($minutes,2, '0');
  56.    
  57.     }
  58.  
  59.     function select($fieldname, $label="Please Choose a timezone") {
  60.    
  61.         $list = $this->Form->input($fieldname, array("type"=>"select", "label"=>$label, "options"=>$this->generateList(), "error"=>"Please choose a timezone"));
  62.         return $this->output($list);
  63.     }
  64.  
  65.     function display($index) {
  66.         return $this->output($this->timezones[$index]);
  67.     }
  68. }
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement