Location: PHPKode > scripts > TimeTaMeR > timetamer/TimeTaMeR.php
<?php

class Timetamer
{

/*******************************************************************************
	TimeTaMeR Class version 0.3
	Copyright (c) 2007, Dennis T. Kaplan
	All rights reserved.
********************************************************************************
	Redistribution and use in source and binary forms, 
	with or without modification, are permitted provided that the following conditions are met:

	    * Redistributions of source code must retain the above copyright notice, 
	    this list of conditions and the following disclaimer.
	    
	    * Redistributions in binary form must reproduce the above copyright notice, 
	    this list of conditions and the following disclaimer in the documentation and/or 
	    other materials provided with the distribution.
	    
	    * Neither the name of the finalwebsites.com nor the names of its contributors may 
	    be used to endorse or promote products derived from this software without specific 
	    prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	_________________________________________________________________________
	If you need help:
	http://www.phpclasses.org/browse/author/288082.html
****************************************************************************/

	var $time;
	var $gmt;
	var $dbzones;
	var $rtime;

	function setVars(){
		$this->time = time();
		$this->gmt = $this->gmt($this->time);
		$this->dbzones = $this->getDBzone();
		$this->getRemoteTime();
	}
	
	function Timetamer(){
		$this->setVars();
		log_message('debug', "TimeTaMeR Class Initialized");
	}
	
	function codes(){
		$zones = $this->getTimeZones();
		foreach($zones as $k => $v){
			if($v->DST == 1){
				$codes->DST = $v->Code;
			}else{
				$codes->NoDST = $v->Code;
			}
		}
		return $codes;
	}

	function checkDST(){
		$dt = date_default_timezone_get();
		date_default_timezone_set('America/Los_Angeles');
		$LAtime = time();
		$LAgmt = $this->gmt($LAtime);
		$dif = ($LAtime - $LAgmt) / 3600;
		date_default_timezone_set($dt);
		if($dif != -8){
			return TRUE;
		}else{
			return FALSE;
		}
	}

	function getRemoteTime()
	{
		$time = $_COOKIE['timeis'];
		$time = explode(",", $time);
		$h = $time[0];
		$m = $time[1];
		$s = $time[2];
		$M = $time[3];
		$D = $time[4];
		$Y = $time[5];
		$time = mktime($h,$m,$s,$M,$D,$Y,-1);
		$gmt = $this->gmt($time);
		$roffset = ($time - $gmt) / 3600; // Get the gmt remote time offset
		$this->rtime = $roffset;
	}


	function getTimeZones()
	{
		$roffset = $this->rtime;
		if($this->checkDST() == TRUE){
			$roffset = $roffset + 1;
		}
		foreach($this->dbzones as $k => $v){
			if($v->GMT == $roffset){
				$rzones[$k] = $v;
			}
		}
		return $rzones;
		
	}
	
	function MakeForm(){
		$codes = $this->codes();
		$DTS   = $codes->DST;
		$NoDTS = $codes->NoDST;
		$form  = "<table><tr><td>DST: </td><td>\n";
		$form .= "<input type=\"radio\" name=\"code\" value=\"$DTS\"> Yes<br /> \n"; 
		$form .= "<input type=\"radio\" name=\"code\" value=\"$NoDTS\"> No \n";
		$form .= "</td></tr></table>";
		return $form;
	}
	
	function getDBzone()
	{
		$link = mysql_connect(DBHOST, DBUSER, DBPASS);
		if (!$link) {
   		die('Could not connect: ' . mysql_error());
   		exit;
		}

		if (!mysql_select_db(DBTABLE)) {
   		print "Unable to select mydbname: " . mysql_error();
   		exit;
   	}
   	
   	$query = "SELECT * FROM `timezone`";
		$result = mysql_query($query);
		if (!$result) {
		   print "<br />Could not successfully run query ($sql) from DB: <br />" . mysql_error() ."<br />";
		   exit;
		}

		if (mysql_num_rows($result) == 0) {
		   print "<br />No rows found, nothing to print so am exiting.<br />";
		   exit;
		}
		while ($row = mysql_fetch_assoc($result)) {
		    $rr[] = $row;
		}
   	   	
   	mysql_close($link);
   	return $rr;
   }

	function gmt($time){
		return mktime( gmdate("H", $time), 
							gmdate("i", $time), 
							gmdate("s", $time), 
							gmdate("m", $time), 
							gmdate("d", $time), 
							gmdate("Y", $time));
	}
}
?>
Return current item: TimeTaMeR