Get country date and time with GMT difference and display the time in client side like digital clock using php and javascript

I am writing this post in regard to the problem I faced when I had to display the county datetime  and display the time as digital clock  in clients computer. When I was new to the php programming , I was not aware of gmt time but however  I get done with problem in a stupid manner.  I would like to define how I made it worked.  First I took the server current time and mapped with my current country time to find  the difference . Then I deducted  that time difference with the server time to meet my country time and made it like digital clock using javascript. There was nothing problem with the javascript but it was with the technique I followed to get my country time. I was not aware of the problem this technique can bring in future unless I met  Roshan Bhattarai He asked me in one interview  ‘have you ever tried with getting the country time and displaying it into clients machine using server side script ? ”  and I confidently answered ‘Yes I have and described him the method I followed’ . But he surprisingly asked ‘ what if your server will get shifted to some other location’ . Then I realized the problem with the method I followed and he made me aware of gmt time and its usage in php.

Here I have written a simple class to get the country date by providing the GMT time difference and simple javascript to display the time as digital clock in clients computer.



DEMO

<?
/* Author Prabeen Giri <http://prabeengiri.com.np> <prabeen.giri@gmail.com> */		
class CountryDate
{
	 var $differenceTime 	        = "";
	 var $countryDate	 	= "" ;
	 var $fullDate 			= "" ;
	 var $hour 	                = "" ;
	 var $minute 			= "" ;
	 var $second 			= "" ;  
 
	//constructor
	function countryDate ( $strTime )
	{
		// assigining the parameter to the  member variable
 
		$this->differenceTime 	= $strTime;
		$date 		        = strtotime($this->differenceTime, strtotime(gmdate('Y-m-d g:i:s A ')) );
		$this->countryDate 	= date( 'Y m d g:i:s' 	, $date	) ;
		$this->fullDate 	= date ('Y m d' , $date ) ;
		$this->hour 		= date ('g' , $date ) ;
		$this->minute 		= date ('i' , $date ) ;
		$this->second 		= date ('s' , $date ) ; 
 
	} 
 
	public function createDigitalClock ( $elementId  )
	{
		echo   '<script>function DigitalClock() '.
			  '{
				var _min 	= '.$this->minute.';'.
				'var _hour 	= '.$this->hour.';'.
				'var _second 	= '.$this->second.';'.
 
				'function showClock ()
				{
				  if ( _second > 59 ){_second = 0;_min++;}if (_min  > 59 ){_min=0;_hour++;}if (_hour > 12 ){_hour = 1;}
				  var _hours 	= (_hour >= 0 && _hour < 10 )? "0" + _hour : _hour;
				  var _mins 	= (_min >= 0 && _min < 10 )? "0" + _min : _min;
				  var _seconds  = (_second >= 0 && _second < 10 ) ? "0" + _second : _second;
				  document.getElementById( "'.$elementId.'").innerHTML = _hours + " : " + _mins + " : " + _seconds ;
				  ++ _second;
				}'.
 
				'this.causeTimeout  =  function(){setInterval( showClock , 1000 );} }
				var dg =  new DigitalClock();dg.causeTimeout(); </script>';
	}
 }
?>

In the above class I have included the javascript function inside the php class to help it deploy easily with the php object. In the javascript code I have used the  javascript object oriented programming approach to display time as digital clock .

How to use:

// GMT Difference as parameter  , used one is for Nepal
$cd = new CountryDate("+5 hours 45 minutes");

This will create the instance of class CountryDate , initializes  other member variables  and please pass the parameter as the string which is the gmt difference of particular country .

$cd->countryDate;    // will return full Date and time
 
$cd->fullDate;       // will return date only
 
$cd->hour;           // will return hour only
 
$cd->minute;         // will return  minute
 
$cd->second;         // will return  second

If you want to make the digital clock of the time then use

// initialize the object and member variable with parameter as GMT time difference
$cd = new CountryDate("+5 hours 45 minutes");
 
// provide the Id of element where you want you digital clock to be displayed. Put this script just above the body close tag.
$cd->createDigitalClock('elementId');



DEMO

1,180 views
Javascript, Php

Comments

10 Responses to “Get country date and time with GMT difference and display the time in client side like digital clock using php and javascript”

Leave Comment

(required)

(required)