Jump to content

Server side clock


codrgi

Recommended Posts

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?

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.