检查此链接 http://tinyurl.com/l96z2m2
它使用 javascript 检索客户端设备时间,使用 php 检索服务器时间。延迟是网络往返时间,即发送请求和收到响应之间的时间。
我想在同一点比较两次。不得不忽略网络延迟。
应该可以通过计算“在客户端发送的请求”和“在服务器收到的请求”之间的时间或“在服务器收到的请求”和“在客户端收到的响应”之间的时间。
索引.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\"
})";
?>