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.
<? /* 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');


Hi, I am interested in your script, but it didn’t show up on my php page. any advice? thx before
can u let me know how you used the script in detail.
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 ‘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 = 0 && _min = 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(); ‘;
}
}
?>
Untitled Document
Country Date and time : March 10, 2010 23:30:48
createDigitalClock(‘elementId’);
?>
I can’t paste the code here, but here is what i do.
i put class CountryDate on the top of test.php. and put
$cd = new CountryDate(“+5 hours 45 minutes”);
$cd->createDigitalClock(‘elementId’);
but it won’t show up. Thank you for your help before.
I got your problem .
You have to do this:
Make the span or div with Id=’digiClock’
<span id=’digiClock’></span>
$cd = new CountryDate(“+5 hours 45 minutes”);
$cd->createDigitalClock(‘digiClock’);
Note: Make sure if want to display the digital clock or invoke the member function ‘createDigitalClock’ , Then invoke it just before the body close tag.
………………………………..
$cd->createDigitalClock(‘digiClock’);
</body>
I hope this will help .
Yes. Now It’s working. Thank you very much Prabeen.
Pleasure is all mine.
thanks so much for the script (i’m a newbie
). Works great and easy to understand/implement/modify for my needs.
Thank you
Hey can you give me advice of how to display date like you showed it in your demo like this : Country Date Only : September 12, 2011 i m waiting for your response .
@abdullah , first create the instance of the above class
.ie.$cd = new CountryDate(“+5 hours 45 minutes”); where argument for constructor(+5 hours 45 minutes) is the GMT time difference. Put if accordling for your respective country . Then to display the country date only
invoke the property of the object that is “countryDate”
Eg. print $cd->countryDate;
I hope this helps