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.
Asked
Active
Viewed 1e+01k times
19

d_4dinkey
- 731
- 1
- 6
- 12
-
Mandatory reading: http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor – John Dvorak Nov 29 '13 at 01:01
-
linked from https://www.c-sharpcorner.com/forums/get-current-time-from-server-side-in-javascript – ashleedawg Aug 08 '22 at 11:46
4 Answers
28
Answering my own question
I found what I was looking for on Webdeveloper.com and it worked excellently for me.
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!!

Albert-Jan
- 315
- 1
- 3
- 11

d_4dinkey
- 731
- 1
- 6
- 12
-
17Remember to post credit where credit is due. You obviously did not code this, thus you should post credits: [original answer credits](http://www.webdeveloper.com/forum/showthread.php?228309-Getting-server-date-time-with-no-server-side-script) – AndreasHassing Nov 28 '13 at 16:17
-
2Dear may be you are exprienced bu i did not posted the code to get credit but for the sake of those who are like me. Ofcourse i did not coded this but i answered my question with what i was looking for. May be it could be helful for someone. – d_4dinkey Nov 28 '13 at 16:24
-
3Sure. But put credit where credit is due, like I said. This has nothing to do with experience. – AndreasHassing Nov 28 '13 at 16:39
-
First of all i figured out and found the solution before even you replied my question. Secondly, nobody always code himself to help someone. Neither you, me or anybody else. But everybody does when required. The aim is to help someone with one way or the other. I am not here to get credits but i always put credits as you are also credited. Cheers mate. – d_4dinkey Nov 28 '13 at 21:51
-
15I gave you the credits, that it takes me this long to explain to you what to do with it baffles me. Simply, edit your 'answer' and write that you got the code from here: http://www.webdeveloper.com/forum/showthread.php?228309-Getting-server-date-time-with-no-server-side-script . And you're wrong, many people DO code themselves. Actually most of the people on stackoverflow code themselves, and Ask when they run into trouble with THEIR code. – AndreasHassing Nov 29 '13 at 00:50
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42161/discussion-between-andreas-bjorn-and-d-4dinkey) – AndreasHassing Nov 29 '13 at 02:15
-
1I am getting this warning in chrome. Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/. – Musakkhir Sayyed Jan 23 '17 at 06:55
-
1@Musakkhir Sayyed: The problem is that when a synchronous XMLHttpRequest gets executed while the page is still loading everything is halted and waits for said request to complete. If the resource that is requested cannot be retrieved in due time (e. g. because of network failures), the request has to time out before everything else is resumed. The better approach is to add a listener on 'readystatechange' to await completion of the request. The remainder can then continue to load while the XMLHttpRequest is still processed. – Robidu Aug 19 '18 at 04:35
-
1And don't forget to set the third parameter in the call to open() to _true_ to make the call asynchronous. – Robidu Aug 19 '18 at 04:50
13
function getServerTime() {
return $.ajax({async: false}).getResponseHeader( 'Date' );
}
console.log('Server Time: ', getServerTime());
console.log('Locale Time: ', new Date(getServerTime()));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Marcelo Ribeiro
- 331
- 3
- 6
4
This example will try to display server time every second.
<html>
<head>
<title></title>
<script type="text/javascript">
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);
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var st = srvTime();
var x = new Date(st); //new Date()
document.getElementById('ct').innerHTML = x;
tt=display_c();
}
</script>
</head>
<body onload=display_ct();>
<span id='ct' ></span>
</body>
</html>
SOURCE1
http://www.plus2net.com/javascript_tutorial/clock.php Note the above example is php free.
SOURCE2

user584583
- 1,242
- 4
- 18
- 35
-
2This code fragment initiates a network connection to the server every second. Aside from the problem that this could trigger firewalls of the server continuously being queried, this is a waste of bandwidth. Progress of time should be kept client-side to avoid superfluous HTTP connections... – Robidu Aug 19 '18 at 04:26
2
Funny enough, while looking for information about Codeigniter to see if I could answer your question or not, I found a runnable that does exactly what you need:
http://runnable.com/UXczcazDrMMiAAGl/how-to-do-ajax-in-codeigniter-for-php
In short, the above script uses Codeigniter and jQuery to ask the server for its current time (via AJAX).
Happy coding.

AndreasHassing
- 687
- 4
- 19
-
Well, we suppose to use this forum to get help or to help suggest others for their questions. I am new to web development and may be my question is funny for you. But you should not use those words but you should help suggest if you can. I am new and anybody new can ask such kind of question. Please try to deal everyone nicely. Its my request. – d_4dinkey Nov 28 '13 at 16:04
-
Not sure what you're talking about. I did not see anything funny in your question. The `runnable` that I linked to has the functionality you need.. Read up on it and you'll be able to use it in your own code. – AndreasHassing Nov 28 '13 at 16:14
-
I answerd my question with what i was actually looking for. I just wanted to know the way to get web server's time and display it on my website's pages. – d_4dinkey Nov 28 '13 at 16:18
-
7@d_4dinkey The phrase "Funny enough" doesn't mean he thinks your question is funny but to express his amusement to the coincidence . its translation from http://dictionary.cambridge.org "strangely, in a way that is surprising" – Fahad Alduraibi Sep 30 '15 at 22:36