You can calculate the exact time using NTP (Network Time Protocol) in your codes,
I will try to explain to you:
- We have ClientTime when sending a request (for example, 4/3/2012 13: 56: 10.123)
- You send ClientTime to the server
- We have Time in the opposite direction for the request, I called it RequestTime (for example: it takes 5 seconds).
- In the server, we calculate the difference time between the server and the client (for example: It ServerTime - ClientTime = ServerClientDifferenceTimeWithRequestTime), you should now this Difference, including the round trip request time in step 3, then you must remove the round-trip time from the Difference
- Send a server response that includes ServerClientDifferenceTimeWithRequestTime and ServerTime
- We have Travel time , I called it ResponseTime (for example: it takes 3 seconds)
- In the client, we again calculate the time difference between the server and the client (for example: It ServerTime - ClientTime = ServerClientDifferenceTimeWithResponseTime), again: now you need this difference, including the response time in the opposite direction in step 6
- We have time in the client
- You have to calculate simple equations in the client:
X (SyncedTime) = Now + (ServerClientDifferenceTimeWithRequestTime - RquestTime)
X (SyncedTime) = Now + (ServerClientDifferenceTimeWithResponseTime - ResponseTime)
Now - ClientTime = RquestTime + ResponseTime =>
Now - (ServerClientDiffRq - RquestTime) = Now - (ServerClientDiffRs - ResponseTime)
if you solve it, you have found this:
ResponseTime = (ServerClientDifferenceTimeWithRequestTime - Now + ClientTime + - ServerClientDifferenceTimeWithResponseTime )/2
and then you can find the synchronized time or server time in the client with this equation:
X (SyncedTime) = Now + (ServerClientDifferenceTimeWithResponseTime - ResponseTime)
I am showing a simple code, but when you want to write it, remember to use the UTC date and time functions ...
Server side (e.g. php, C #):
PHP:
header('Content-Type: application/json; charset=utf-8'); $clientTime = $_GET["ct"] * 1; //for php 5.2.1 or up: (float)$_GET["ct"]; $serverTimestamp = round(microtime(true)*1000); // (new DateTime())->getTimestamp(); $serverClientRequestDiffTime = $serverTimestamp - $clientTime; echo "{\"diff\":$serverClientRequestDiffTime,\"serverTimestamp\":$serverTimestamp}";
FROM#
long clientTime = long.Parse(Request.Form["ct"]); long serverTimestamp = (DateTime.Now.Ticks-(new DateTime(1970,1,1) - DateTime.MinValue).Ticks) / 10000; long serverClientRequestDiffTime = serverTimestamp - clientTime; Response.Write("{\"diff\":"+serverClientRequestDiffTime+",\"serverTimestamp\":"+serverTimestamp+"}");
Client side (Javascript with Jquery ):
var clientTimestamp = (new Date()).valueOf(); $.getJSON('http://yourhost.com/getdatetimejson/?ct='+clientTimestamp, function( data ) { var nowTimeStamp = (new Date()).valueOf(); var serverClientRequestDiffTime = data.diff; var serverTimestamp = data.serverTimestamp; var serverClientResponseDiffTime = nowTimeStamp - serverTimestamp; var responseTime = (serverClientRequestDiffTime - nowTimeStamp + clientTimestamp - serverClientResponseDiffTime )/2 var syncedServerTime = new Date((new Date()).valueOf() + (serverClientResponseDiffTime - responseTime)); alert(syncedServerTime); });