...

View Full Version : Grabbing the current time on the server (not client)??



OFAH
05-08-2003, 10:13 PM
Is it possible to use JavaScript to get the current time on the server (not the current Client side time).

If so, what is the syntax??

Thank you in advance.

brothercake
05-08-2003, 11:35 PM
It's not possible in javascript. Can you run a server-side language?

OFAH
05-09-2003, 03:01 PM
I am pretty sure I can. I can run either PHP or Cold Fusion. I know how to get the current time with Cold Fusion but not how to incorporate it into the script to make the time live (running clock, ticking numbers, etc....).

requestcode
05-09-2003, 03:16 PM
Your question was though if you could do it with javascript. Brothercake is correct. You probably should ask this in the PHP or Server Side languages forums. There should be someone there that can help. Good Luck.

arnyinc
05-09-2003, 03:30 PM
I'm pretty sure you can figure it out by using the server's time zone offset (which you can set set statically), the client's time zone offset, and the client's current time.

Something with getTimezoneOffset() I think.

Here's an old script I found that should help. No matter what you change your time zone too, it will keep the correct time based on offsets.

<html>

<head>
<script language="javascript" type="text/javascript">
//borrowed from echoecho
//http://www.echoecho.com/ubb/viewthread.php?tid=2362&pid=10482&#pid10482
workDate = new Date()
UTCDate = new Date()
UTCDate.setTime(workDate.getTime()+workDate.getTimezoneOffset()*60000)

function printTime(offset) {
offset++;
tempDate = new Date()
tempDate.setTime(UTCDate.getTime()+3600000*(offset))
timeValue = ((tempDate.getHours()<10) ? ("0"+tempDate.getHours()) : (""+tempDate.getHours()))
timeValue += ((tempDate.getMinutes()<10) ? ("0"+tempDate.getMinutes()) : tempDate.getMinutes())
timeValue += " hrs."
return timeValue
}
var now = new Date()
var seed = now.getTime() % 0xfffffff
var same = rand(12)
</script>

</head>

<body>
Banff, Canada:
<script language="JavaScript">document.write(printTime("-7"))</script>
<br>
Michigan:
<script language="JavaScript">document.write(printTime("-5"))</script>
<br>
Greenwich, England(UTC):
<script language="JavaScript">document.write(printTime("-0"))</script>
<br>
Tokyo, Japan:
<script language="JavaScript">document.write(printTime("+9"))</script>
<br>
Berlin, Germany:
<script language="JavaScript">document.write(printTime("+1"))</script>

</body>
</html>

boggly
05-29-2003, 06:29 AM
There's a simple SSI directive that can do what you mentioned.

CFMX RULES
05-30-2003, 12:17 AM
id wrap this up as a custom tag and call it that way, but anyhow here's the CF code you require



<meta http-equiv="REFRESH" content="150"><HTML><BODY bgcolor=eeeeee><HEAD>
<cfoutput>
<cfset tnow=#now()#>
<cfset snow=#DatePart("s", "#tnow#")#+4>

<!--- the +4 seconds above is the latency due to internet communications. Typically, this should be set to 3 seconds. About 1 second for internet latency and about 2 seconds to execute the javascript on the first access. We offer a dynamic clock (at extra cost) by monitoring the users latency period and dynamically adjusting the clock time to accomodate latency/CPU.--->

<script language="JavaScript">
// 1-----------Javascript developed by LabX.
// 2-----------Copyright protected
// 3-----------By Bob Kafato and Fred Plotnikoff info@labx.com
// 4-----------These four comment lines must be present and unaltered

function setTime() {
curhour = "#DatePart("h", "#tnow#")#";
curminutes = "#DatePart("n", "#tnow#")#";
curseconds = "#snow#";
setClock(curhour, curminutes, curseconds);
}

function setClock(ch, cm, cs) {
// -----------this function calculates the new time and tracks it---
CurHour = parseInt(ch);
timeHour = CurHour;
if (CurHour == 24) {
CurHour = 0;
timeHour= 0;
}
if (CurHour < 10) {
CurHour = "0" + CurHour;
}
CurMinute = parseInt(cm);
if (parseInt(cm) < 10) {
CurMinute = "0" + parseInt(cm);
}
else CurMinute = parseInt(cm);

if (parseInt(cs) < 10) {
CurSeconds = "0" + parseInt(cs);
}
else
CurSeconds = parseInt(cs);

if (CurHour >= 12) {
timeHour = CurHour - 12;
if (timeHour < 10) {
timeHour = "0" + timeHour;
Ampm = "pm";
}
else
Ampm = "pm";
}
else
{
timeHour = CurHour;
Ampm = "am";
}

if (timeHour == 0) {
timeHour = "12";
}

CurMinute = ""+CurMinute;
CurHour = ""+CurHour;
Time = timeHour + ":" + CurMinute + ":" + CurSeconds + " " + Ampm;
document.clocktext.clock.value = Time;
CurSeconds = parseInt(cs) + 1;
CurSeconds = ""+CurSeconds;
CurMinute = parseInt(cm);
CurHour = parseInt(ch);

if (parseInt(CurSeconds) > 59) {
CurMinute = parseInt(cm) + 1;
CurMinute = ""+CurMinute;
CurSeconds = 0;
CurSeconds = parseInt(CurSeconds);
}

if (parseInt(CurMinute) == 60) {
CurHour = parseInt(CurHour) + 1;
CurMinute = "0";
CurMinute = parseInt(CurMinute);
}

// document.clocktext.clock.value = Time;
setTimeout ("setClock(CurHour, CurMinute, CurSeconds)", 1000 * 1);
}

</script>

</cfoutput>
<TITLE>Server Clock</TITLE>
</HEAD>
<BODY bgcolor=eeeeee ONLOAD="setTime()">
<cfoutput>
<FORM NAME="clocktext">
<b>
<font color=000000 face=arial size=2>Server Clock: #DateFormat("#now()#", "mmm dd")#</font>
<font size=3> &nbsp;<input type="text" name="clock" size="10" value=""></b></FORM></font></cfoutput>
</body></html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum