Click to See Complete Forum and Search --> : Changing Time every minut not second


miha
03-30-2006, 05:07 AM
I have JavaScript code and each city time should change evary minut but not with clients PC time but with server time with PHP. Do you have any suggestions?

Code for time is:




<!-- ############################## -->
<!-- ##### Start of HTML Code ##### -->

<table border="0" cellspacing="2" cellpadding="0" class="worldTimeTable" style="border-style: solid; border-color: #A0A0A0; border-width: 1px;">
<tr>
<td id="FrankfurtDT" class="worldTimeTable"></td>
<td width="10"></td>
<td id="TokyoDT" class="worldTimeTable"></td>
</tr>
</table>
<!-- ##### End of HTML Code ##### -->
<!-- ############################ -->


<!-- #################################### -->
<!-- ##### Start of JavaScript Code ##### -->

<script language="JavaScript">
var timerID ;
function tzone(tz, os, ds, cl)
{
this.ct = new Date(0) ; // datetime
this.tz = tz ; // code
this.os = os ; // GMT offset
this.ds = ds ; // has daylight savings
this.cl = cl ; // font color
}

function UpdateClocks()
{
var ct = new Array(

new tzone('<b>Frankfurt:</b> ', 1, 1, ''),
new tzone('<b>Tokyo:</b> ', +9, 0, '')
) ;

var dt = new Date() ; // [GMT] time according to machine clock
var startDST = new Date(dt.getFullYear(), 3, 1) ;
while (startDST.getDay() != 0)
startDST.setDate(startDST.getDate() + 1) ;

var endDST = new Date(dt.getFullYear(), 9, 31) ;
while (endDST.getDay() != 0)
endDST.setDate(endDST.getDate() - 1) ;

var ds_active ; // DS currently active
if (startDST < dt && dt < endDST)
ds_active = 1 ;
else
ds_active = 0 ;

// Adjust each clock offset if that clock has DS and in DS.
for(n=0 ; n<ct.length ; n++)
if (ct[n].ds == 1 && ds_active == 1) ct[n].os++ ;

// compensate time zones
gmdt = new Date() ;
for (n=0 ; n<ct.length ; n++)
ct[n].ct = new Date(gmdt.getTime() + ct[n].os * 3600 * 1000) ;


document.getElementById("FrankfurtDT").innerHTML = ct[4].tz + ClockString(ct[4].ct);
document.getElementById("TokyoDT").innerHTML = ct[5].tz + ClockString(ct[5].ct);

timerID = window.setTimeout("UpdateClocks()", 60000) ;
}

function ClockString(dt)
{
var stemp, ampm ;
var dt_year = dt.getUTCFullYear() ;
var dt_month = dt.getUTCMonth() + 1 ;
var dt_day = dt.getUTCDate() ;
var dt_hour = dt.getUTCHours() ;
var dt_minute = dt.getUTCMinutes() ;
dt_year = dt_year.toString() ;

if (dt_minute < 10)
dt_minute = '0' + dt_minute ;
if (dt_hour < 10)
dt_hour = '0' + dt_hour ;

stemp = getMonthText(dt_month) + ' ' + dt_day + ' ' ;
stemp = stemp + ' ' + dt_hour + ":" + dt_minute ;
return stemp ;
}

function getMonthText(tempMon)
{
switch (tempMon)
{
case 1: return "Jan"; break;
case 2: return "Feb"; break;
case 3: return "Mar"; break;
case 4: return "Apr"; break;
case 5: return "May"; break;
case 6: return "Jun"; break;
case 7: return "Jul"; break;
case 8: return "Aug"; break;
case 9: return "Sep"; break;
case 10: return "Oct"; break;
case 11: return "Nov"; break;
case 12: return "Dec"; break;
}
}
UpdateClocks();
</script>
<!-- ##### End of JavaScript Code ##### -->

TF@TheMoon
03-30-2006, 06:16 AM
You want the client side code to referesh from data on the server without refreshing the page? You'll need to use xmlHttpRequest.

I, personally, use AJAX and the prototype.js (http://prototype.conio.net/) script, as that is what they build into Ruby on Rails, but for what you want its probably overkill.

This might be a good read to start http://www.xml.com/pub/a/2005/02/09/xml-http-request.html

But google on xmlHttpRequest for other.

Basically you'l need your Javascript to trigger the xmlHttpRequest ever minute that calls a PHP script that simply returns the server time. this wil then be picked up by your javascript code to do with as it wants