我正在尝试使用PHP
和AJAX
来使用web服务器的日期和时间。问题是,PHP
给出不同的时间,AJAX
给出不同的。我搞不清我的代码出了什么问题。下面是我的代码。
AJAX
function srvTime() {
var xmlHttp;
try {
//FF, Opera, Safari, Chrome
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//IE
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//AJAX not supported, use CPU time.
alert("AJAX not supported");
}
}
}
xmlHttp.open('HEAD', window.location.href.toString(), false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}
document.write("Server Time and Date through AJAX: "+srvTime());
var phpDate = "<?php echo Date('d-m-Y H:i:s')?>"
document.write("Server Time and Date through PHP"+phpDate);
输出
Server Time and Date through AJAX Mon, 09 Dec 2013 21:11:28 GMT //AJAX
Server Time and Date through PHP 09-12-2013 16:11:28 // PHP
请帮帮我。谢谢
所有HTTP日期/时间戳必须以格林尼治标准时间表示(GMT),无一例外(来源)
Ajax响应为GMT格式PHP Date返回服务器的时间。您可以使用检查服务器时区
date_default_timezone_get()
或检查php.ini 中的"date.timezone"选项