このリンクをチェックしてください http://tinyurl.com/l96z2m2
javascript を使用してクライアント デバイスの時間を取得し、php を使用してサーバー時間を取得します。delay はネットワークの往復時間、つまり、送信された要求と受信された応答の間の時間です。
同じ時点で両方の時間を比較したい。ネットワーク遅延を無視する必要があります。
「クライアントで送信された要求」と「サーバーで受信された要求」の間の時間、または「サーバーで受信された要求」と「クライアントで受信された応答」の間の時間のいずれかを計算することで可能になるはずです。
index.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
<script>
var b = new Date();
$.getJSON("http://localhost/json.php?callback=?", function(data){
var a = new Date();
document.write("|Client-"+a.getUTCHours()+":"+a.getUTCMinutes()+":"+a.getUTCSeconds()+":"+a.getUTCMilliseconds()+"| Server-"+data.time);
var c = a - b;
document.write("|delay-"+c);
});
</script>
json.php
<?php
$callback = $_GET["callback"];
function udate($format, $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
$t = udate('H:i:s:u');
echo $callback . "({
\"time\":\"$t\"
})";
?>