我希望在一个php/javascript应用程序中使用服务器时间。对于该应用程序,精确到秒的时间非常重要。然而,尽管检查了客户端和服务器时间之间的时间差异,仍然存在3-4秒的差异。
我的Angular代码如下:
// Pre ajax time (so we can see how long it took to get data from server)
var timeElapsed = new Date().getTime();
$http.get('/getServerTime.php')
.success(function (data) {
// Calculate the length of the ajax request in seconds
timeElapsed = new Date().getTime() - timeElapsed;
// Add that to the returned server time to get the current
// server time. (data.date is provided by the getServerTime.php page)
$scope.serverTime = data.date + timeElapsed;
$scope.timeDifference = new Date().getTime() - $scope.serverTime;
});
理论上这应该可以工作,但是出现了高达四秒的差异。如有建议或代码修改,请不吝赐教。