PDA

View Full Version : How to show server time in java script code?



toplisek
03-30-2006, 10:05 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 ##### -->

Twey
03-30-2006, 12:46 PM
var dt = new Date() ; // time according to machine clock
dt.setHours(<?php echo(date("G")); ?>);
dt.setMinutes(<?php echo(date("i")); ?>);

toplisek
03-31-2006, 08:36 AM
I have put:



var dt = new Date() ; // time according to machine clock
dt.setHours(<?php echo(date("G")); ?>);
dt.setMinutes(<?php echo(date("i")); ?>);
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() ;


1. Question: Do you know why it shows me always the same time
Can be changed each minut?
and
2. Qestion: it is in all cities the same value. what is wrong?

I hope you have some suggestions

jscheuer1
03-31-2006, 08:51 AM
Have you looked into:

Local Time script (http://www.dynamicdrive.com/dynamicindex6/localtime.htm)


Description: Want to display the current local time of a specific place in the world? This DHTML script, with the help of either PHP, ASP, or SSI (server side includes), displays the current server time your site is hosted on, and in turn, the local time of anywhere on Earth. You can use it to show visitors your current local time, or the time in New York, Tokyo etc. Now, in order to use this script, the webpage you're adding it to must be either PHP (ie: .php), ASP, or SSI (ie: .shtml) enabled. More on this below

toplisek
03-31-2006, 09:35 AM
this is very good. I have tested, but do not know if I can change view of time?
I would like to have it 16:15

So, time should be (without AM or PM and date):
HOUR:MINUTES

Twey
03-31-2006, 10:03 AM
This line:
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"Change to:
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)

toplisek
03-31-2006, 10:19 AM
Change to:
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)

It works, but time have to be SHORT

Thanks

Twey
03-31-2006, 10:41 AM
I have no idea what you're talking about.
The script doesn't work unless the time is short?
You want the time to be output in a "short" form? If so, how exactly do you want it to be shorter than hh:mm format?

toplisek
03-31-2006, 11:10 AM
I would like to show time in format: HH:MM, format should be 24 h and without AM or PM and date.

I have changed code to this as you told me:


showLocalTime.prototype.updateContainer=function(){
var thisobj=this
if (this.displayversion=="long")
this.container.innerHTML=this.localtime.toLocaleString()
else{
var hour=this.localtime.getHours()
var minutes=this.localtime.getMinutes()
var seconds=this.localtime.getSeconds()
var ampm=(hour>=12)? "PM" : "AM"
var dayofweek=weekdaystxt[this.localtime.getDay()]
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)


and to display (tokyo time):
new showLocalTime("timecontainer7", "server-php", 540, "long") //GMT +9

What to do that it wil show tokyo time like Hours:Minutes (24 hour format)

Twey
03-31-2006, 11:22 AM
Aha. Find:

if (typeof isHour!="undefined"){ //if this is the hour field
var hour=(num>12)? num-12 : num
return (hour==0)? 12 : hour
}Replace with:
if (typeof isHour!="undefined")
return (hour < 10 ? "0" + hour : hour);

toplisek
03-31-2006, 12:58 PM
It does not work.
This will show not values:
Current Server Time:
London:
Frankfurt:
Toronto:
Chicago:
New York:
Tokyo:

simonf
03-31-2006, 01:25 PM
Hi

I take it your want to show the time in various cities around the world?? one way would be to use the getTimezoneOffset() command.. see this thread for a hint:


time thread (http://www.dynamicdrive.com/forums/showthread.php?t=1103&highlight=time)

Twey
03-31-2006, 01:43 PM
Sorry, I made a mistake.
if (typeof isHour!="undefined")
return (num < 10 ? "0" + num : num);
Simonf: We got past that bit. :)

toplisek
03-31-2006, 05:23 PM
Can you please tell me the whole code. It seems it is not correct:
I need for showing cities time as this (format 24h just time without date):
Please help.:)



Current Server Time:<span id="timecontainer"></span><br />
London:<span id="timecontainer2"></span><br />
Frankfurt:<span id="timecontainer3"></span><br />
Toronto: <span id="timecontainer4"></span><br />
Chicago: <span id="timecontainer5"></span><br />
New York: <span id="timecontainer6"></span><br />
Tokyo: <span id="timecontainer7"></span><br />

<script type="text/javascript">
new showLocalTime("timecontainer", "server-php", 0, "long") //GMT -5


new showLocalTime("timecontainer2", "server-php", 300, "long") //GMT +1
new showLocalTime("timecontainer3", "server-php", 420, "long") //GMT +2
new showLocalTime("timecontainer4", "server-php", 0, "long") //GMT -5
new showLocalTime("timecontainer5", "server-php", -60, "long") //GMT -6
new showLocalTime("timecontainer6", "server-php", 0, "long") //GMT -5
new showLocalTime("timecontainer7", "server-php", 540, "long") //GMT +9


</script>

Twey
03-31-2006, 05:45 PM
The third parameter is the offset in minutes, I believe. So GMT +1 should be (5 + 1) * 60 == 360; GMT +9 should be (5 + 9) * 60 = 545.

toplisek
03-31-2006, 09:10 PM
Thanks for time. Please help me how to do correct above code for format 24 h.
So, e.g. 19:45 (Hour:Minutes)

Twey
03-31-2006, 09:29 PM
If you correct the offsets, that code should be fine.

toplisek
03-31-2006, 09:32 PM
If you correct the offsets, that code should be fine.

Do you mean code:


function formatField(num, isHour){
if (typeof isHour!="undefined"){ //if this is the hour field
var hour=(num>12)? num-12 : num
return (hour==0)? 12 : hour
}


changed to:



if (typeof isHour!="undefined")
return (num < 10 ? "0" + num : num);


this will not work. Can you please give correct code for this?

Twey
03-31-2006, 09:37 PM
No,
if (typeof isHour!="undefined"){ //if this is the hour field
var hour=(num>12)? num-12 : num
return (hour==0)? 12 : hour
}changed to
if (typeof isHour!="undefined")
return (num < 10 ? "0" + num : num);and
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"to
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes);

toplisek
04-01-2006, 07:30 AM
I do not know but it does not work.

I post whole code. Please give me whole code for time format 24hours

I'm confused.:confused:



<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">

/***********************************************
* Local Time script- Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]

function showLocalTime(container, servermode, offsetMinutes, displayversion){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.displayversion=displayversion
var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
this.localtime=this.serverdate=new Date(servertimestring)
this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
this.updateTime()
this.updateContainer()
}

showLocalTime.prototype.updateTime=function(){
var thisobj=this
this.localtime.setSeconds(this.localtime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

showLocalTime.prototype.updateContainer=function(){
var thisobj=this
if (this.displayversion=="long")
this.container.innerHTML=this.localtime.toLocaleString()
else{
var hour=this.localtime.getHours()
var minutes=this.localtime.getMinutes()
var seconds=this.localtime.getSeconds()
var ampm=(hour>=12)? "PM" : "AM"
var dayofweek=weekdaystxt[this.localtime.getDay()]
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes);

}
setTimeout(function(){thisobj.updateContainer()}, 60000) //update container every second
}

function formatField(num, isHour){
if (typeof isHour!="undefined")
return (num < 10 ? "0" + num : num);
}
return (num<=9)? "0"+num : num//if this is minute or sec field
}

</script>


Current Server Time:<span id="timecontainer"></span><br />
London:<span id="timecontainer2"></span><br />
Frankfurt:<span id="timecontainer3"></span><br />
Toronto: <span id="timecontainer4"></span><br />
Chicago: <span id="timecontainer5"></span><br />
New York: <span id="timecontainer6"></span><br />


<script type="text/javascript">
new showLocalTime("timecontainer", "server-php", 0, "long") //GMT -5


new showLocalTime("timecontainer2", "server-php", 300, "long") //GMT +1
new showLocalTime("timecontainer3", "server-php", 420, "long") //GMT +2
new showLocalTime("timecontainer4", "server-php", 0, "long") //GMT -5
new showLocalTime("timecontainer5", "server-php", -60, "long") //GMT -6
new showLocalTime("timecontainer6", "server-php", 0, "long") //GMT -5



</script>