Location: PHPKode > scripts > The Country Date > the-country-date/date_country.php
<?php
/*
TheCountryDate Class v. 1.3
By Sarawut Lorvijit
Created : 12/04/2009
Modified : 01/05/2009
Please send e-mail to me (hide@address.com) for your interest in this.

Test on PHP : 
	5.0.0, 5.0.5, 5.1.0, 5.1.1, 5.1.3, 5.1.6, 5.2.0, 5.2.9-2
	5.1.0 : If use class name "date" will display the message "Fatal error: Cannot redeclare class date in ..."

Method
	get_date() : Host date, such as 2005-08-15T15:52:01+00:00 or 2005-08-15T15:52:01+0000
	get_timezone() : Host timezone, such as Asia/Krasnoyarsk (if PHP < 5.1.0 return empty string)
	get_local_date() : Your country date, such as 2009-04-30T00:22:58+07:00
	get_local_timezone() : Your country timezone (config in C_LOCAL_TIMEZONE)
	get_local_hour() : Your country hour, such as 7, 14 or 0
	get_local_minute() : Your country minute, such as 1, 30 or 0
	get_local_week_day() : Your country weekday, the value is in 0, 1, 2, ... 6
	get_local_day() : Your country day, such as 1, 16, 30 or 31
	get_local_month() : Your country month, such as 1, 7 or 12
	get_local_year() : Your country year, such as 2009, 2010
	get_local_diff_zero() : Your country time that more or less than GMT, the value is "+" or "-"
	get_local_diff_hour() : Your country different hour from GMT, such as 7, 14 or 0
	get_local_tz_abbr() : Your country abbreviated timezone
	get_tz_abbr() : Host abbreviated timezone
	get_week_day_compre() : comprehensive week day that pass the parameter from 0 to 6
*/
class date_country extends base{
	const C_VERSION = 1.3;
	const C_FORMAT_A = 0;
	const C_FORMAT_B = 1;
	
	const C_LOCAL_TIMEZONE = "Asia/Bangkok";
	const C_LOCAL_DIFF_GMT = 7;	//hour
	const C_LOCAL_DIFF_GMT_MIN = 0;	//minute
	const C_LOCAL_TZ_ABBR = "ICT";
	
	const C_SET_DATE_H = 0;	//host
	const C_SET_DATE_L = 1;	//local ( country)
	
	function __construct($p_bln_uploaded = false, $p_str_default_format = self::C_FORMAT_A){
		parent::__construct($p_str_default_format);
		
		$this->m_data['timezone'] = $this->_set_timezone($p_bln_uploaded);
		$this->_set_date();
		$this->_set_date(1);	//local date
	}
	
	function __destruct(){
		parent::__destruct();
	}
	
	/*
	return
		PHP 5.1.6 : date : 2009|04|26|21:09:46|+07:00|0|ICT
		PHP 5.0.5 : date : 2009|04|28|01:00:13|-1200|2|Dateline Standard Time
	
	note : 
	date("e") PHP 5.1.0
	date("P") PHP 5.1.3
	Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT : PHP 5.1.0
	01-01-1970 to 19-01-2038 : PHP < 5.1.0
	*/
	private function _set_date($p_int_option = self::C_SET_DATE_H){
		$prefix = "";
		if($p_int_option==self::C_SET_DATE_L){	//local date
			$prefix = "local_";
		}
		
		if(PHP_VERSION>=parent::C_VERSION_1_3){
			if($p_int_option==self::C_SET_DATE_H){
				$this->m_data[$prefix.'date'] = date('Y|m|d|H:i:s|P|w|T');
			}elseif($p_int_option==self::C_SET_DATE_L){
				$this->m_data[$prefix.'date'] = date('Y|m|d|H:i:s|P|w|T', $this->_get_gmt_date_time_stamp() + (self::C_LOCAL_DIFF_GMT * 3600));
			}
		}else{
			if($p_int_option==self::C_SET_DATE_H){
				$this->m_data[$prefix.'date'] = date('Y|m|d|H:i:s|O|w|T');
			}elseif($p_int_option==self::C_SET_DATE_L){
				$this->m_data[$prefix.'date'] = date('Y|m|d|H:i:s|O|w|T', $this->_get_gmt_date_time_stamp() + (self::C_LOCAL_DIFF_GMT * 3600));
			}
		}
		
		$arr_date_part = split('[|:]', $this->m_data[$prefix.'date']);
		$this->m_data[$prefix.'year'] = $arr_date_part[0];
		$this->m_data[$prefix.'month'] = $arr_date_part[1];
		$this->m_data[$prefix.'day'] = $arr_date_part[2];
		$this->m_data[$prefix.'hour'] = $arr_date_part[3];
		$this->m_data[$prefix.'minute'] = $arr_date_part[4];
		$this->m_data[$prefix.'second'] = $arr_date_part[5];
		if($p_int_option==self::C_SET_DATE_H){
			$this->m_data[$prefix.'diff_zero'] = substr($arr_date_part[6], 0, 1);	//6 : PHP >= 5.1.3 : +07, PHP < 5.1.3 : +0700
			$this->m_data[$prefix.'diff_hour'] = substr($arr_date_part[6], 1, 2);
		}elseif($p_int_option==self::C_SET_DATE_L){
			if(self::C_LOCAL_DIFF_GMT>=0){
				$this->m_data[$prefix.'diff_zero'] = "+";
			}else{
				$this->m_data[$prefix.'diff_zero'] = "-";
			}
			$diff_hour = abs(self::C_LOCAL_DIFF_GMT);			
			$this->m_data[$prefix.'diff_hour'] = (strlen($diff_hour)<2)?"0".$diff_hour:$diff_hour;
		}
		if(PHP_VERSION>=parent::C_VERSION_1_3){
			if($p_int_option==self::C_SET_DATE_H){
				$this->m_data[$prefix.'diff_minute'] = $arr_date_part[7];
				$this->m_data[$prefix.'week_day'] = $arr_date_part[8];
				$this->m_data[$prefix.'tz_abbr'] = $arr_date_part[9];
			}elseif($p_int_option==self::C_SET_DATE_L){
				$this->m_data[$prefix.'diff_minute'] = (strlen(self::C_LOCAL_DIFF_GMT_MIN)<2)?"0".self::C_LOCAL_DIFF_GMT_MIN:self::C_LOCAL_DIFF_GMT_MIN;
				$this->m_data[$prefix.'week_day'] = $arr_date_part[8];
				$this->m_data[$prefix.'tz_abbr'] = self::C_LOCAL_TZ_ABBR;
			}
		}else{
			if($p_int_option==self::C_SET_DATE_H){
				$this->m_data[$prefix.'diff_minute'] = substr($arr_date_part[6], 3, 2);
				$this->m_data[$prefix.'week_day'] = $arr_date_part[7];
				$this->m_data[$prefix.'tz_abbr'] = $arr_date_part[8];
			}elseif($p_int_option==self::C_SET_DATE_L){
				$this->m_data[$prefix.'diff_minute'] = (strlen(self::C_LOCAL_DIFF_GMT_MIN)<2)?"0".self::C_LOCAL_DIFF_GMT_MIN:self::C_LOCAL_DIFF_GMT_MIN;
				$this->m_data[$prefix.'week_day'] = $arr_date_part[7];
				$this->m_data[$prefix.'tz_abbr'] = self::C_LOCAL_TZ_ABBR;
			}
		}
	}
	
	//GMT date&time
	private function _get_gmt_date_time_stamp(){
		$int_month = (int)($this->m_data['month']);
		$int_day = (int)($this->m_data['day']);
		$int_year = (int)($this->m_data['year']);
		if($this->m_data['diff_zero']=="+"){
			$result = mktime($this->m_data['hour'], $this->m_data['minute'], $this->m_data['second'], $int_month, $int_day, $int_year) - ($this->m_data['diff_hour'] * 3600);
			//gmmktime($this->m_data['hour'], $this->m_data['minute'], $this->m_data['second'], $int_month, $int_day, $int_year);	&#3649;&#3626;&#3604;&#3591;&#3648;&#3623;&#3621;&#3634;&#3652;&#3617;&#3656;&#3606;&#3641;&#3585;&#3605;&#3657;&#3629;&#3591;&#3585;&#3621;&#3634;&#3618;&#3648;&#3611;&#3655;&#3609;&#3648;&#3585;&#3636;&#3609; 7 &#3594;&#3633;&#3656;&#3623;&#3650;&#3617;&#3591;
		}elseif($this->m_data['diff_zero']=="-"){
			$result = mktime($this->m_data['hour'], $this->m_data['minute'], $this->m_data['second'], $int_month, $int_day, $int_year) + ($this->m_data['diff_hour'] * 3600);
		}
		
		return $result;
	}
	
	/*
	set timezone on host
	return
		timezone : Asia/Krasnoyarsk, Asia/Bangkok, America/New_York, ...
		false : can not st timezone
	
	note : 
	php.ini directive date.timezone PHP 5.1.0
	date_default_timezone_get(), date_default_timezone_set() PHP 5.1.0
	*/
	private function _set_timezone(&$p_bln_uploaded){
		$result = false;
		if(PHP_VERSION>=parent::C_VERSION_1){
			$str_ini_timezone = ini_get('date.timezone');
			
			//ini_get failure
			if(!$p_bln_uploaded){
				//not uploaded
				//date_default_timezone_get()	return "Asia/Krasnoyarsk"
				$str_default_timezone = self::C_LOCAL_TIMEZONE;
			}else{
				$str_default_timezone = date_default_timezone_get();	//none of succeed, return UTC, return string only
				if($str_default_timezone==""){	//error only
					$str_default_timezone = self::C_LOCAL_TIMEZONE;
				}
			}
			$result = $str_default_timezone;
			
			if(is_string($str_ini_timezone)){
				if($str_ini_timezone!=""){
					//ini_get success
					$result = $str_ini_timezone;
				}
			}
			
			date_default_timezone_set($result);
		}
		
		return $result;
	}
	
	//public
	function version(){
		return self::C_VERSION;
	}
	
	function get_week_day_compre($p_int_week_day){
		$result = false;
		if($p_int_week_day>=0&&$p_int_week_day<=6){
			$arr_week_day = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");	//parameter "w" of date
			$result = $arr_week_day[$p_int_week_day];
		}
		
		return $result;
	}
	
	function get_tz_abbr(){
		return $this->m_data['tz_abbr'];	//host timezone abbr.
	}
	
	function get_local_tz_abbr(){
		return $this->m_data['local_tz_abbr'];
	}
	
	function get_local_diff_hour(){
		return (int)($this->m_data['local_diff_hour']);
	}
	
	function get_local_diff_zero(){
		return (int)($this->m_data['local_diff_zero']);
	}
	
	function get_local_year(){
		return (int)($this->m_data['local_year']);
	}
	
	function get_local_month(){
		return (int)($this->m_data['local_month']);
	}
	
	function get_local_day(){
		return (int)($this->m_data['local_day']);
	}
	
	function get_local_week_day(){
		return (int)($this->m_data['local_week_day']);
	}
	
	function get_local_minute(){
		return (int)($this->m_data['local_minute']);
	}
	
	function get_local_hour(){
		return (int)($this->m_data['local_hour']);
	}
	
	function get_local_timezone(){
		return self::C_LOCAL_TIMEZONE;
	}
	
	function get_local_time(){
		return $this->m_data['local_hour'].":".$this->m_data['local_minute'];
	}
	
	//return your country date
	function get_local_date($p_int_abbr=0){
		if($p_int_abbr==0){
			$result = $this->m_data['local_year'].'-'.$this->m_data['local_month'].'-'.$this->m_data['local_day'].'T'.$this->m_data['local_hour'].':'.$this->m_data['local_minute'].':'.$this->m_data['local_second'].$this->m_data['local_diff_zero'].$this->m_data['local_diff_hour'].':'.$this->m_data['local_diff_minute'];
		}elseif($p_int_abbr==1){
			$result = $this->m_data['local_year'].'-'.$this->m_data['local_month'].'-'.$this->m_data['local_day'];
		}
		
		return $result;
	}
	
	//Asia/Krasnoyarsk
	function get_timezone(){
		return $this->m_data['timezone'];
	}
	
	/*
	return
		2005-08-15T15:52:01+00:00 (C_FORMAT_A)
		2005-08-15T15:52:01+0000 (C_FORMAT_B)
	*/
	function get_date(){
		$result = $this->m_data['year'].'-'.$this->m_data['month'].'-'.$this->m_data['day'].'T'.$this->m_data['hour'].':'.$this->m_data['minute'].':'.$this->m_data['second'].$this->m_data['diff_zero'].$this->m_data['diff_hour'].':'.$this->m_data['diff_minute'];
		if($this->m_data['initial']==self::C_FORMAT_B){
			$result = $this->m_data['year'].'-'.$this->m_data['month'].'-'.$this->m_data['day'].'T'.$this->m_data['hour'].':'.$this->m_data['minute'].':'.$this->m_data['second'].$this->m_data['diff_zero'].$this->m_data['diff_hour'].$this->m_data['diff_minute'];
		}
		return $result;
	}
}
?>
Return current item: The Country Date