codrgi Posted April 27, 2009 Share Posted April 27, 2009 My code function zeroPad(num,count) { var numZeropad = num + ''; while(numZeropad.length < count) { numZeropad = "0" + numZeropad; } return numZeropad; } function getLocalTime() { var rightNow = new Date(); var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); var temp = date1.toGMTString(); var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); var temp = date2.toGMTString(); var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60); var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60); if (hoursDiffDaylightTime == hoursDiffStdTime) { return "GMT" + hoursDiffStdTime; } else { return "GMT" + hoursDiffStdTime + " <small>DST</small>"; } } function getTimezone(){ var d= new Date(); var offs= d.getTimezoneOffset(); var str= offs> 0? '+': ''; str+= offs%60? offs+' minutes, ': (offs/60)+' hours, '; var s= d.toString(); var tz= /(\: *\d{2} *)([a-z]+)([\-\+]\d+)? *\(?([a-z ]+)?/i.exec(s) || []; if(!tz[3]) tz[4]= tz[2]; if(offs==0) return tz[4] || ' GMT'; return ' GMT '+str+ (tz[4] || ''); } function countdownTimer(gotTime,elementName) { // Set my element get var idInfo = document.getElementById(elementName); var elementHour = elementName+'_hour'; var idInfoHour = document.getElementById(elementHour); // Write up my date functions, now and then var d = new Date(); var d2 = new Date(gotTime); // Time in miliseconds, for now and then var nowLocalTime = d.getTime(); var thenLocalTime = d2.getTime(); // global offset, should be the same for BOTH var offsetTime = (d.getTimezoneOffset()*(60000)); // Convert thenTime & nowTime to GMT time var thenTime = thenLocalTime+offsetTime; var nowTime = nowLocalTime+offsetTime; // Get the hour/minute/sec in...local...time... var localHours = zeroPad(d2.getHours(),2); var localMinutes = zeroPad(d2.getMinutes(),2); var localSeconds = zeroPad(d2.getSeconds(),2); // Calculae the difference diff = (thenTime-nowTime); // Get our varables/minutes/seconds/hours/mili/days var milliseconds=Math.floor(diff % 1000); diff=diff/1000; var seconds=Math.floor(diff % 60); diff=diff/60; var minutes=Math.floor(diff % 60); diff=diff/60; var hours=Math.floor(diff % 24); diff=diff/24; var days=Math.floor(diff); // Add leading zero hours = zeroPad(hours,2); minutes = zeroPad(minutes,2); seconds = zeroPad(seconds,2); // Colour? if(hours < 1) { // Hour return hourReturn = '<span style="color: red;">'+hours+':'+minutes+':'+seconds; // Write our final return var returnVar = ''+hours+':'+minutes+':'+seconds+'</span>'; } else { // Hour Return hourReturn = ''; // Write our final return var returnVar = ''+hours+':'+minutes+':'+seconds; } // Return our results if(idInfo) { idInfo.innerHTML = returnVar; } if(idInfoHour) { idInfoHour.innerHTML = hourReturn; } above is a countdown timer, but it uses the clients clock and not server side clock, how would i solve this? Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/ Share on other sites More sharing options...
Psycho Posted April 27, 2009 Share Posted April 27, 2009 You would want to use server-side code (e.g. PHP) to write a JavaScript variable of the current server time. Then the JavaScript would increment that time as it runs. Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-820614 Share on other sites More sharing options...
codrgi Posted April 28, 2009 Author Share Posted April 28, 2009 im both new to js and php, how would i do that? Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-820807 Share on other sites More sharing options...
Psycho Posted April 28, 2009 Share Posted April 28, 2009 http://www.phpfreaks.com/forums/index.php/topic,223869.0.html Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-820836 Share on other sites More sharing options...
codrgi Posted April 28, 2009 Author Share Posted April 28, 2009 i tried adding php date('G'); using my code but now it dosen't show anything Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821152 Share on other sites More sharing options...
Psycho Posted April 28, 2009 Share Posted April 28, 2009 Not to be rude, but how do you expect any help when you don't provide any code. There are any number of things causing your problem. Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821246 Share on other sites More sharing options...
codrgi Posted April 29, 2009 Author Share Posted April 29, 2009 I posted a js code in my first post, however i'll now post the one that has php in it and giving me the probs, i hope someone will brush it up for me function zeroPad(num,count) { var numZeropad = num + ''; while(numZeropad.length < count) { numZeropad = "0" + numZeropad; } return numZeropad; } function getLocalTime() { var rightNow = <?php date('G');?>; var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); var temp = date1.toGMTString(); var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); var temp = date2.toGMTString(); var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60); var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60); if (hoursDiffDaylightTime == hoursDiffStdTime) { return "GMT" + hoursDiffStdTime; } else { return "GMT" + hoursDiffStdTime + " <small>DST</small>"; } } function getTimezone(){ var d= <?php date('G');?>; var offs= d.getTimezoneOffset(); var str= offs> 0? '+': ''; str+= offs%60? offs+' minutes, ': (offs/60)+' hours, '; var s= d.toString(); var tz= /(\: *\d{2} *)([a-z]+)([\-\+]\d+)? *\(?([a-z ]+)?/i.exec(s) || []; if(!tz[3]) tz[4]= tz[2]; if(offs==0) return tz[4] || ' GMT'; return ' GMT '+str+ (tz[4] || ''); } function countdownTimer(gotTime,elementName) { var idInfo = document.getElementById(elementName); var elementHour = elementName+'_hour'; var idInfoHour = document.getElementById(elementHour); // Write up my date functions, now and then var d = <?php date('G');?>; var d2 = new Date(gotTime); // Time in miliseconds, for now and then var nowLocalTime = d.getTime(); var thenLocalTime = d2.getTime(); // global offset, should be the same for BOTH var offsetTime = (d.getTimezoneOffset()*(60000)); // Convert thenTime & nowTime to GMT time var thenTime = thenLocalTime+offsetTime; var nowTime = nowLocalTime+offsetTime; // Get the hour/minute/sec in...local...time... var localHours = zeroPad(d2.getHours(),2); var localMinutes = zeroPad(d2.getMinutes(),2); var localSeconds = zeroPad(d2.getSeconds(),2); diff = (thenTime-nowTime); // Get our varables/minutes/seconds/hours/mili/days var milliseconds=Math.floor(diff % 1000); diff=diff/1000; var seconds=Math.floor(diff % 60); diff=diff/60; var minutes=Math.floor(diff % 60); diff=diff/60; var hours=Math.floor(diff % 24); diff=diff/24; var days=Math.floor(diff); // Add leading zero hours = zeroPad(hours,2); minutes = zeroPad(minutes,2); seconds = zeroPad(seconds,2); if(hours < 1) { // Hour return hourReturn = '<span style="color: red;">'+hours+':'+minutes+':'+seconds; // Write our final return var returnVar = ''+hours+':'+minutes+':'+seconds+'</span>'; } else { // Hour Return hourReturn = ''; // Write our final return var returnVar = ''+hours+':'+minutes+':'+seconds; } // Return our results if(idInfo) { idInfo.innerHTML = returnVar; } if(idInfoHour) { idInfoHour.innerHTML = hourReturn; } Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821522 Share on other sites More sharing options...
radi8 Posted April 29, 2009 Share Posted April 29, 2009 Here is my initial thoughts... I may be wrong, but i do not think you can mix-mode javascript and php, so the lines with <?php date('G');?>; are bogus. what you may need to do is change the countdownTimer(...) to the following and pass the server time into the function via html and php. Process all of the other functions as you are already doing, but use the <?php date('G');?> in the function calling routine for the serverTime, i.e. countdownTimer(<?php date('G');?>,getlocalTime(), elementname); It would also help if you posted the html code that is calling this javascript. We can then play with it to get it working. function zeroPad(num,count) { var numZeropad = num + ''; while(numZeropad.length < count) { numZeropad = "0" + numZeropad; } return numZeropad; } function getLocalTime() { var rightNow = new Date(); var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); var temp = date1.toGMTString(); var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); var temp = date2.toGMTString(); var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60); var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60); if (hoursDiffDaylightTime == hoursDiffStdTime) { return "GMT" + hoursDiffStdTime; } else { return "GMT" + hoursDiffStdTime + " <small>DST</small>"; } } function getTimezone(){ var d= new Date(); var offs= d.getTimezoneOffset(); var str= offs> 0? '+': ''; str+= offs%60? offs+' minutes, ': (offs/60)+' hours, '; var s= d.toString(); var tz= /(\: *\d{2} *)([a-z]+)([\-\+]\d+)? *\(?([a-z ]+)?/i.exec(s) || []; if(!tz[3]) tz[4]= tz[2]; if(offs==0) return tz[4] || ' GMT'; return ' GMT '+str+ (tz[4] || ''); } function countdownTimer(serverTime, gotTime, elementName) { var idInfo = document.getElementById(elementName); var elementHour = elementName+'_hour'; var idInfoHour = document.getElementById(elementHour); // Write up my date functions, now and then var d = new Date(serverTime); var d2 = new Date(gotTime); // Time in miliseconds, for now and then var nowLocalTime = d.getTime(); var thenLocalTime = d2.getTime(); // global offset, should be the same for BOTH var offsetTime = (d.getTimezoneOffset()*(60000)); // Convert thenTime & nowTime to GMT time var thenTime = thenLocalTime+offsetTime; var nowTime = nowLocalTime+offsetTime; // Get the hour/minute/sec in...local...time... var localHours = zeroPad(d2.getHours(),2); var localMinutes = zeroPad(d2.getMinutes(),2); var localSeconds = zeroPad(d2.getSeconds(),2); diff = (thenTime-nowTime); // Get our varables/minutes/seconds/hours/mili/days var milliseconds=Math.floor(diff % 1000); diff=diff/1000; var seconds=Math.floor(diff % 60); diff=diff/60; var minutes=Math.floor(diff % 60); diff=diff/60; var hours=Math.floor(diff % 24); diff=diff/24; var days=Math.floor(diff); // Add leading zero hours = zeroPad(hours,2); minutes = zeroPad(minutes,2); seconds = zeroPad(seconds,2); if(hours < 1) { // Hour return hourReturn = '<span style="color: red;">'+hours+':'+minutes+':'+seconds; // Write our final return var returnVar = ''+hours+':'+minutes+':'+seconds+'</span>'; } else { // Hour Return hourReturn = ''; // Write our final return var returnVar = ''+hours+':'+minutes+':'+seconds; } // Return our results if(idInfo) { idInfo.innerHTML = returnVar; } if(idInfoHour) { idInfoHour.innerHTML = hourReturn; } Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821796 Share on other sites More sharing options...
Psycho Posted April 29, 2009 Share Posted April 29, 2009 I may be wrong, but i do not think you can mix-mode javascript and php, so the lines with <?php date('G');?>; are bogus. Yes and no. There's no reason you cannot dynamically create JavaScript using PHP just like you can dynamically create HTML with PHP. However, you are right in that <?php date('G');?>; is bugs. Well, it's not technically bogus, but it won't do anything! You need to put an echo in there to have it actually written to the page. <?php echo date('G');?>; I have not analyzed the rest of the code. But, you shouldn't need to write that same variable three times to the page. Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821908 Share on other sites More sharing options...
codrgi Posted April 29, 2009 Author Share Posted April 29, 2009 I tried the php echo and still having problems with this, all i want the code to do is get the servers time, not the clients time Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821928 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2009 Share Posted April 29, 2009 codrgi, Can you post the code you have now? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-821934 Share on other sites More sharing options...
Psycho Posted April 29, 2009 Share Posted April 29, 2009 I tried the php echo and still having problems with this, all i want the code to do is get the servers time, not the clients time And, the URL I posted to the other thread shows you how to do that. There are two problems with what you have done. 1. You are only outputting a number (the hour) from the server and you are trying to use it as a date object. 2. The rest of your javascript has other problems based upon the limited testing I did. If you can provide a complete working page where you "hard code" a server-time, I can provide a solution. But, I can't determine what you are trying to do in your code, so I can't provide a solution. Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-822097 Share on other sites More sharing options...
Psycho Posted April 29, 2009 Share Posted April 29, 2009 Here is a sample script that will display the server time and clinet time on the page. <html> <head> <script type="text/javascript"> var serverTime = new Date(<?php echo (time() * 1000); ?>); var clientTime = new Date(); var serverOffset = serverTime - clientTime; function showTime() { // alert(serverOffset); var client = new Date(); var server = new Date(client - serverOffset); document.getElementById('client').innerHTML = client; document.getElementById('server').innerHTML = server; setTimeout('showTime()', 1000); } </script> </head> <body onload="showTime();"> Client Time: <span id="client"></span><br> Server Time: <span id="server"></span> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/155857-server-side-clock/#findComment-822178 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.