Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scenario:

Servers systems are all over the world.
Web page on the client machine is requesting data.

Issue:

How to know the time zone offset between any server and any client machine?

Work:

JavaScript
var DocumentTime = new Date();
function GetTimeZoneOffset()
	{
		var nTime = DocumentTime.getTime();
		var sUrl = xxxPhpUrl + '?TimeZoneOffSet=' + nTime;
		if(window.XMLHttpRequest)
		{
			var xhr = new XMLHttpRequest();
			xhr.open("GET", sUrl, true);
			xhr.onreadystatechange = function()
			{
				if (xhr.readyState == 4)
				{}
			}
			xhr.timeout = 30000;
			xhr.ontimeout = TimeOut;
			xhr.send(null);
		}
		nNow = null;
		nTime = null;
		sUrl = null;
	}

<body  önload="GetTimeZoneOffset()"


PHP::

PHP
if (isset($_GET['TimeZoneOffSet']))
{
	$ClientTime = $_GET['TimeZoneOffSet'];
	$ServerTime = time();
	$ServerTime = $ServerTime*1000;
	$Diff = $ServerTime - $ClientTime;
	echo $Diff;
	echo " Server ";
	echo $ServerTime;
	echo " and client ";
	echo $ClientTime;
}


Webpage Posts a timestamp to the php on the server (using get date function) and php should take the timestamp and give difference between the webpage timestamp and its local timestamp.

Output:
The difference is hardly 70000 microseconds. even though the server timezone is in PST and client is in EST. Is this correct?

How do i achieve this?
Posted
Updated 24-Jun-14 10:54am
v2

1 solution

Calculating the time difference between two location is not a good thing. In particular, it may change, because in different location there can be a different policy about summer saving time (who needs it anyway? such an archaic pointless trick…) which can change with time.

I would advise the following approach: do all the programming, on all severs and all clients, in UTC time: http://en.wikipedia.org/wiki/UTC[^].

This manual page explains how to use it in PHP: http://www.php.net//manual/en/function.time.php[^].

…and this one — in Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC[^].

And only in one point, in the client-side script, only in showing time in UI, convert UTC time to local. This way, you never need to know the timezone of the server, its summer saving time, anything; you will only need to convert universal UTC time to the client host local time. Having correct local time on the host is a purely local concern, so you won't need any coordination between hosts (everything is already coordinated by UTC :-)), won't need any redundant knowledge in time setting on any of them.

—SA
 
Share this answer
 
Comments
amarasat 24-Jun-14 17:51pm    
I have a server which returns 2 hours of data. A webpage reads this data and displays on a trend. Now when i read the data and display it, the data is off by three hours because client is in EST and server is in PST. If i can get the time difference between server and client i can shift my data on the trend by that many hours.

One way is
On Client, Date.getTimezoneOffset() is giving the offset from UTC and GMT. Is there any function in php that gives same type of offset on the server side? If i echo that, i can get the difference in times.
markkuk 25-Jun-14 6:20am    
Use DateTime::getOffset() in PHP http://www.php.net/manual/en/datetime.getoffset.php
amarasat 25-Jun-14 17:22pm    
Can you give me an example on how to do it? Is this correct, am i getting the offset between the current server and the GMT?

$ServerTimeZone = date_default_timezone_get();
$ServerDateTimeZone = new DateTimeZone($ServerTimeZone);
$Offset = new DateTime('now', $ServerDateTimeZone);

echo $Offset->getOffset();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900