Location: PHPKode > scripts > bbWorldTime > bbworldtime/bbWorldTime.class.php
<?
/*
###############################################################################
    WorldTime Class 1.3
    ---------------------------------------------------
    Author: K.Bogac Bokeer
    EMail : hide@address.com
    
    Thanks: (for GetForeignTime Function Code)
         W_TIME v1.0 The Official World Time Script!
         Made by Matt Emery at B-scene Web design www.b-scene.com.au 
         Please email hide@address.com for any info
              about our scripts and services. 
###############################################################################
*/

class bbWorldTime {

    var $filename       = 'timezones.lst';       // TimeZones file
    var $zones;                                  // Zone List (array)

    var $localzone      = 'Server';              // Default Time Zone
    var $localtimedef   = 0;                     // Default Time Zone Difference
    var $foreignzone;

    var $localtime      ='--:--';                // Stores Local Time
    var $localhour;                              // Stores Local Hour
    var $localminute;                            // Stores Local Minute


    
    // Constructor function of class
    function bbWorldTime($localzone='', $filename='timezones.lst') {

         if ( !empty($filename) ) 
             $this->filename = $filename;

         $this->zones = $this->GetZones();

         if ( !empty($localzone) ) {
    
             $this->localzone    = $localzone;

             $this->InitLocal();

             $this->localtimedef = $this->GetCityZone($this->zones, $this->localzone);
             
         }
         
    }

    
    function SetLocaleZone($localzone='') {
    
         if ( !empty($localzone) ) {

              $this->localzone    = $localzone;

              $this->localtimedef = $this->GetCityZone($this->zones, $this->localzone);
              
              return true;

         } else

              return false;

    }

    
    function GetLocalZone() {
    
         return $this->localzone;

    }


    function GetLocalTime() {
    
         return $this->localtime;

    }

    
    function GetTimeForCity($city) {

         $city_time = $this->GetCityZone($this->zones, $city);

         return $this->GetForeignTime($this->localtimedef, $city_time);

    }
    
    
    function WriteSelect($name='city', $selected='', $html_space=3) {

         if ( !is_array($this->zones) ) return false;
         
         $space = str_repeat(' ', $html_space);
         
         echo $space."<select name=\"$name\">\n";
         echo $space."   <option>--- select a city ---</option>\n";
         
         for ( $i=0; $i<count($this->zones);$i++ ) {

              echo $space."   <option value=\"".$this->zones[$i]['name']."\"";
              if ( strtoupper($this->zones[$i]['name']) == strtoupper($selected) ) echo " SELECTED";
              echo ">".$this->zones[$i]['name']."</option>\n";
              
         }
         
         echo $space."</select>\n";

         return true;
    }

    
    function DisplayZones($TDClass='', $highlight_city='', $highlight='', $ColumnCount=3, $html_space=6) {
    
         $count = count($this->zones);
    
         $CountPerColumn = (int)($count/$ColumnCount);
         if ( ($count % $ColumnCount)>0 ) $CountPerColumn++;
    
         $space = str_repeat(' ', $html_space);
    
         echo "\n$space<!--  ZONES CLASS -->\n$space<table cellspacing='0' cellpadding='2' border='0'>\n";

         $bgcolor = '#EEEEEE';
         
         for ($i=0; $i<$CountPerColumn; $i++) {
         
              if ( $bgcolor=='FFFFFF' )
                   $bgcolor='EEEEEE';
              else
                   $bgcolor='FFFFFF';
         
              echo "$space   <tr>\n";

              for ( $col=0; $col<$ColumnCount; $col++ ) {
         
                   $index = $i+($CountPerColumn * $col);
              
                   if ( $index<$count ) {
              
                        $city = $this->zones[$index]['name'];
                        $time = $this->GetTimeForCity($city);
                   
                        if ( (!empty($highlight)) && ($city==$highlight_city) )
                             $td_class=" class='$highlight'";
                        else if ( !empty($TDClass) ) 
                             $td_class = " class='$TDClass'";
                        else
                             $td_class = '';
                   
                        echo "$space      <td width='110'$td_class bgcolor='#$bgcolor'><nobr><b>$city</b></nobr></td>\n";
                        echo "$space      <td width='40' align='right'$td_class bgcolor='#$bgcolor'>$time</td>\n";
                        if  ( $col<($ColumnCount-1) ) echo "$space      <td width='10'>&nbsp;</td>\n";

                   } else {
                   
                        echo "$space      <td colspan='3'>&nbsp;</td>\n";
                   
                   }

              }

              echo "$space   </tr>\n";
         }

         echo "$space</table>\n$space<!--  ZONES END -->\n\n";

    }


    /*
         Private Founctions
    */
    
    function InitLocal() {

         $this->localhour    = (strftime ("%H"));

         $this->localminute  = (strftime ("%M"));

         $this->localtime    = sprintf("%02d", $this->localhour).":".$this->localminute;

         return true;
    }
    
    
    function GetZones() {

         $file = @fopen($this->filename, "r");
         
         if ( !$file ) return false;

         $counter = 0;

         while ( $line = fgetcsv($file, 1024, ";") ) {

              $zones[$counter]['name']    = $line[1];
              $zones[$counter]['timedef'] = $line[0];

              $counter++;
         }
         
         fclose($file);

         return $zones;
    }
    
    
    function GetCityZone($zones, $city) {


         if ( !is_array($this->zones) ) return false;

         for ( $i=0; $i<count($this->zones);$i++ ) {
         
              if ( strtoupper($this->zones[$i]['name'])==strtoupper($city) ) return (int)$this->zones[$i]['timedef'];

         }

         return false;

    }


    function GetForeignTime($time_difference, $city_time) {

         $this->InitLocal();

         $GMT = $this->localhour - $time_difference;  // Fix for your zone

         $newtime = $city_time + $GMT;

         if ($newtime > 23) {
             $time = ($newtime) - 24;
         } elseif ($newtime < 1) {
             $time = ($newtime) + 24;
         } elseif ( ($newtime > 1) || ($newtime < 23) )  {
             $time = $newtime;
         }

         return sprintf("%02d", $time).":".$this->localminute;
    }

}
?>
Return current item: bbWorldTime