正确的方法来获取Web服务器时间并在Web页面上显示它 [英] Right way to get Web Server time and display it on Web Pages
问题描述
我使用 PHP Codeigniter Framework
开发我的网站。我想在我的一些网页上显示我的网络服务器的时间以及客户端的机器时间。我使用 setInterval('clientMachineTime()',1000)$ c>成功显示客户端时间使用
JavaScript
$ c>函数。我想显示Web服务器时间与我的客户端时钟相同的工作。我googled这个,但不能找到我正在寻找什么。任何建议将不胜感激。感谢。
回答自己的问题
我找到了我要找的东西在Webdeveloper.com ,它的工作非常出色。我想分享它,因为它不容易找到。
<$ p $ p>
var xmlHttp;
function srvTime(){
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不支持,使用CPU时间。
alert(AJAX not supported);
}
}
}
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader(Content-Type,text / html);
xmlHttp.send('');
return xmlHttp.getResponseHeader(Date);
}
var st = srvTime();
var date = new Date(st);
html
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>服务器日期/时间< / title>
< script language =javascriptsrc =serverDate.js>< / script>
< / head>
< script language =javascript>
var localTime = new Date();
document.write(本地机器时间是:+ localTime +< br>);
document.write(Server time is:+ date);
< / script>
< body>
< / body>
>
I am developing my website using PHP Codeigniter Framework
. I want to display my webserver's time as well as client's machine time on some of my web pages. I successfully displayed client side time using JavaScript
that updates the time every second by simply using setInterval('clientMachineTime()',1000)
function. I want to display the web server time with the same working of my client side clock. I googled for this but could not find exactly what i am looking for. Any suggestion would be appreciated. Thanks.
Answering my own question
I found what I was looking for on Webdeveloper.com and it worked excellently for me. I want to share it because it was not easy to find. I hope it will be helpful for others.
serverDate.js
var xmlHttp;
function srvTime(){
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");
}
var st = srvTime();
var date = new Date(st);
html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server date/time</title>
<script language="javascript" src="serverDate.js"></script>
</head>
<script language="javascript">
var localTime = new Date();
document.write("Local machine time is: " + localTime + "<br>");
document.write("Server time is: " + date);
</script>
<body>
</body>
Cheers!!
这篇关于正确的方法来获取Web服务器时间并在Web页面上显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!