Zero-Affect Posted July 26, 2010 Share Posted July 26, 2010 <span id="servertime"></span> [js]<script type="text/javascript"> function show() { var timda = '<php? date("F jS h:i:s A") ?>' document.getElementById('servertime').innerHTML = timda; setTimeout("show()",1000) } show() </script>[/js] I know im doing something wrong just can't picture it. I am a complete newb when it comes to JS Quote Link to comment Share on other sites More sharing options...
Zeggy Posted July 26, 2010 Share Posted July 26, 2010 Well, javascript is run in the browser and php is run on the server. When the page loads, the php adds the current time to your var timda, and then you can consider it 'hardcoded' to the javascript. All the function calls you do after that with javascript will load that exact same timestamp, meaning your div won't change at all. You're just reloading the same string into it over and over again. If you really want to use php for the time, you can either use php to give the starting timestamp, and use javascript to increment the time, otherwise you'll need to use ajax to reload the time from php. But there's not really any need for that, javascript has its own time functions. If you want to display time you can do it purely with javascript. Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted July 26, 2010 Author Share Posted July 26, 2010 Wow Zeggy thanks for that pal. EDIT: [js] <SCRIPT LANGUAGE="JavaScript"> <!-- Begin var timerRunning = false; var timezone = "Greenwich Mean Time"; // what time zone are you in ? var adjust = 0; function timeCheck(tzone, diff) { if (timerRunning) { clearTimeout(updatetime); timerRunning = false; } gmtOffset=eval(diff+adjust); timezone = tzone; checkDateTime(); } function checkDateTime () { var today = new Date(); var month = today.getMonth()+1; var date = today.getDate(); var day = today.getDay(); var hour = today.getHours(); var minute = today.getMinutes(); var second = today.getSeconds(); var lastSat = date - (day+1); while (lastSat < 32) lastSat+=7; if (lastSat > 31) lastSat+=-7; var firstSat = date - (day+1); while (firstSat > 0) firstSat+=-7; if (firstSat < 1) firstSat+=7; if ((((month == 4) && (date >= firstSat)) || month > 4) && (month < 11 || ((month == 10) && day <= lastSat))) adjust += 60; yourOffset = (new Date()).getTimezoneOffset(); yourOffset = yourOffset + adjust; var xx = navigator.appName var xy = navigator.appVersion; xy = xy.substring(0,1); if ((xy == 4) && (xx == "Netscape")) yourOffset = yourOffset+adjust; if ((((month == 4) && (date > 20)) || month > 4) && (month < 11 || ((month == 10) && day < 30))) adjust -= 60; ourDifference = eval(gmtOffset - yourOffset); var half = eval(ourDifference % 60); ourDifference = Math.round(ourDifference / 60); hour = eval(hour - ourDifference); if ((half == -30) || (half == 30)) minute += 30; if (minute > 59) minute -= 60, hour++; if (minute < 0) minute += 60, hour--; if (hour > 23) hour -= 24, date += 1; if (hour < 0) hour += 24, date --; var dateTime = hour; dateTime = ((dateTime < 10) ? "0":"") + dateTime; dateTime = " " + dateTime; dateTime += ((minute < 10) ? ":0" : ":") + minute; dateTime += ((second < 10) ? ":0" : ":") + second; dateTime += (hour >= 12) ? " PM" : " AM"; document.getElementById('servertime').innerHTML = dateTime; updatetime=setTimeout("checkDateTime()", 900); timerRunning = true; } // End --> </SCRIPT> [/js] <BODY OnLoad="timeCheck(timezone, +360)"> <span id="servertime"></span> Google'd it and came up with something like that Quote Link to comment Share on other sites More sharing options...
Haunted Dawg Posted July 26, 2010 Share Posted July 26, 2010 Honestly? All that for something so simple? :P Slap this in a file that display's nothing: if(isset($_GET['time'])) { echo date("F jS h:i:s A"); exit; } Now put this wherever you want to display your time: <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $(function() { setInterval("$('#servertime').load('url.php?time=true');",1000); } ); </script> <div id="servertime"></div> Then just change the url to where it is going to be displayed in the JQuery section. :) Quote Link to comment Share on other sites More sharing options...
Danny696 Posted July 26, 2010 Share Posted July 26, 2010 LMAO, big difference in codes there. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted July 26, 2010 Share Posted July 26, 2010 I really didn't think about doing it with jQuery. Here's what I got. <span>This Server Time</span><p id="stime"></p> <script> var sdate = new Date('<?php echo date("F j, Y, g:i:s a"); ?>'); var seconds = sdate.getSeconds(), minutes = sdate.getMinutes(), hours = sdate.getHours(); function serverTime() { seconds++; if(seconds >= 60) { minutes++; seconds = 0; } if(minutes >= 60) { hours++; minutes = 0; } if(hours >= 12) { var sabr = 'PM'; } else { var sabr = 'AM'; } //document.getElementById('stime').innerHTML='Time: '+sdate.getHours()+'.'+sdate.getMinutes()+'.'+sdate.getSeconds()+sabr; document.getElementById('stime').innerHTML='Time: '+hours+'.'+minutes+'.'+seconds+' '+sabr; setTimeout('serverTime()', 1000); } serverTime(); </script> Quote Link to comment Share on other sites More sharing options...
Fathom Posted July 26, 2010 Share Posted July 26, 2010 Using JQuery to query the server every second to get the time? Maybe it is short code but doesn't make it good/clever in any stretch of the imagination-do you get free bandwidth? Why make your server get hit like that? That is a real no imo. You can improve Djkanna's abit to let javascript do the logic (I assuming his works!) and keep time more accurately. <span>This Server Time</span><p id="stime"></p> <script> var sdate = new Date('<?php echo date("F j, Y, g:i:s a"); ?>'); var diff = sdate.getTime() - new Date().getTime(); function serverTime() { sdate.setTime(new Date().getTime()+diff); if(sdate.getHours() >= 12) { var sabr = 'PM'; } else { var sabr = 'AM'; } document.getElementById('stime').innerHTML='Time: '+sdate.getHours()+'.'+sdate.getMinutes()+'.'+sdate.getSeconds()+sabr; setTimeout('serverTime()',1000); } serverTime(); </script> Edit: (edit2: removed edit as it doesnt show 24hr) Quote Link to comment Share on other sites More sharing options...
Danny696 Posted July 26, 2010 Share Posted July 26, 2010 Paul; Look at your first post, you didnt get the <?php part right xD you put <php? Quote Link to comment Share on other sites More sharing options...
Haunted Dawg Posted July 26, 2010 Share Posted July 26, 2010 @Fathom: Please take a look here and see the script's in action. http://deadlykillah.net/folderhidden/test2.php Your's nor DJKanna's seem to work. Quote Link to comment Share on other sites More sharing options...
Zeggy Posted July 26, 2010 Share Posted July 26, 2010 Using JQuery to query the server every second to get the time? Maybe it is short code but doesn't make it good/clever in any stretch of the imagination-do you get free bandwidth? Why make your server get hit like that? That is a real no imo. Exactly, if all that's being done is display time, then the best solution is pure javascript. Apart from bandwidth issues, there's also problems with loading speed, disconnects, latency/inaccuracy, etc. Every browser should be capable of counting its own time :) Quote Link to comment Share on other sites More sharing options...
Djkanna Posted July 26, 2010 Share Posted July 26, 2010 @Fathom: Please take a look here and see the script's in action. http://deadlykillah.net/folderhidden/test2.php Your's nor DJKanna's seem to work. Mine works for me an Paul ?( http://www.deverz.com/time.php Quote Link to comment Share on other sites More sharing options...
Zeggy Posted July 26, 2010 Share Posted July 26, 2010 @Fathom: Please take a look here and see the script's in action. http://deadlykillah.net/folderhidden/test2.php Your's nor DJKanna's seem to work. Theirs requires an initial php timestamp to be passed. If you look at the source, your page doesn't seem to do that, it's actually displaying the php code inside their javascripts :P Quote Link to comment Share on other sites More sharing options...
Haunted Dawg Posted July 26, 2010 Share Posted July 26, 2010 Take a look again and see how more accurate mine is. http://deadlykillah.net/folderhidden/test2.php Quote Link to comment Share on other sites More sharing options...
Danny696 Posted July 26, 2010 Share Posted July 26, 2010 IT just the same as DJK's Quote Link to comment Share on other sites More sharing options...
Fathom Posted July 26, 2010 Share Posted July 26, 2010 Take a look again and see how more accurate mine is. http://deadlykillah.net/folderhidden/test2.php Haunted Dawg, obviously you need to rename the functions and divs for both mine and Djkanna's to work on the same page correctly :). Quote Link to comment Share on other sites More sharing options...
Zeggy Posted July 27, 2010 Share Posted July 27, 2010 Take a look again and see how more accurate mine is. http://deadlykillah.net/folderhidden/test2.php Hmm... let's see... 100 users online at the same time, 60 seconds per minute = 6,000 requests per second just for a simple timer. It's a silly waste of bandwidth and processing power that could be used better elsewhere. And it won't be more accurate than a javascript timer. There's always a latency delay and loading speed delay that you cannot avoid, while a javascript timer relies on the client's own cpu clock. The javascript solutions posted above aren't ideal - they still require php to give the initial time. It would be more accurate to simply use javascript and grab the time straight from the browser. As it is right now, it's starting from the time that php parses that line, and there could be seconds between that event and when the javascript begins running on the client's browser. Also the solutions use setTimeout. The time between each update then becomes 1000ms + whatever time it takes to execute timer update, and eventually the timer would be way out of sync with real time. It would be more accurate to use setInterval. Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted July 27, 2010 Author Share Posted July 27, 2010 actually when i figured PHP would drag it a little with it running every second i assumed it would be easier to just use JS so i use the code i posted a while back with a few edits lol using timezone not unixstamp. Thanks for all the posts though lads:D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.