PDA

View Full Version : Regarding date-time clock ...



lprag
12-20-2005, 04:39 PM
Hello,

Regarding this date-time clock:

http://www.dynamicdrive.com/dynamicindex6/clock3.htm

Is there any way to have it read the time and date from my www host's server rather than read it from the viewer's computer?

i.e: if the viewer's computer time/date is wrong, it shows up that way on my www page. Whereas, I doubt that my host's server will be wrong.

If it is possible to read data from my host's server, is it also possible to figure out what time zone the viewer is in and insert that time rather than the server's time?

Thank you.

jscheuer1
12-20-2005, 04:57 PM
see:

http://www.dynamicdrive.com/dynamicindex6/localtime.htm

lprag
12-20-2005, 05:33 PM
Hello John,

Thank you. Your link is an improvement but ...

there must also be a way to 1) use the server time (as your link does) and 2) have the script figure out from the viewer's computer clock, what time zone they are in and automatically adjust the server time.

Any ideas?

Lowell Prag

jscheuer1
12-20-2005, 08:21 PM
Since the only servers I have to test this on are in and set to my time zone, I cannot be certain this will work but, in theory it is sound. As a result of these modifications, the script will no longer work to get times offset to various zones (more on that later). OK, replace the function 'showLocalTime()' with this one:


function showLocalTime(container, servermode, 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.serverdate=new Date(servertimestring)
this.localtime=new Date(new Date(this.serverdate.toGMTString()).toLocaleString())
this.updateTime()
this.updateContainer()
}

And in the body of the page use this:


Current Time: <span id="timecontainer"></span>

<script type="text/javascript">
new showLocalTime("timecontainer", "server-php", "short")
</script>

What I've done is convert the server time to GMT then to local time. Even if the user's clock is off, as long as their time zone is correctly set, this should result in the server's time being displayed in the user's local time zone format. I would be able to preserve the option of an offset for different zones but it was much easier not to bother with it. So, as you can see, it is no longer one of the parameters. You still must select the correct servermode.

lprag
12-20-2005, 10:08 PM
Hello John,

Thanks. That is exactly what I want.

My problem now is that I don't know how to edit the script, as I know nothing about script language.

With the original http://www.dynamicdrive.com/dynamicindex6/clock3.htm,
all I had to do was copy it and it worked.

For your new script, here's what I don't understand:

1. ContainerID (string): The ID of the DIV or span that will house the local time.

I guess I don't have to change that.

2. Servermode (string): Valid values are either "server-php", "server-asp", or "server-ssi", to indicate this page as either of type, PHP, ASP, or SSI.

This I don't understand. My www page is just simple html and as the note with the script, states, the script won't work with plain html. Is there something I can add to the head section which will keep my page looking the same but also make the script work?

i.e: "make it php, asp, or ssi enabled". I know nothing about what that means.

Unless I can do that, your previous work for me is wasted.

3. LocaltimeoffsetMinutes (integer): The offset in minutes of the local time you wish to display, from the server time. For example, if your server time is currently 9:30 AM and you know the desired local time is 11:30AM, the offset is 120, or 2 hours then.

This I guess no longer exists, per your note.

4. Display Format (string): valid values are either "short" or "long". The latter will cause the local date to be displayed along with the time.

Yes, I want the long format but I am not sure how to edit
the entry.

In short, what I want if possible, is this:

Tuesday, December 20, 2005
Current time: 4:53:01 p.m.

where the 01 changes every second.

Can any of this work with my situation or should
I give up on making this idea work?

Lowell Prag

jscheuer1
12-21-2005, 12:03 AM
For that, you can modify the function showLocalTime.prototype.updateContainer, replace it with this one:


showLocalTime.prototype.updateContainer=function(){
var thisobj=this
this.container.innerHTML=this.localtime.toLocaleDateString()+'<br>'
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+="Current Time: "+formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
}

Now this is all you need in the HTML, wherever you want the time displayed:


<span id="timecontainer"></span>

and below that, have the short script call (unchanged from the previous mod):


<script type="text/javascript">
new showLocalTime("timecontainer", "server-php", "short")
</script>

You are right about the LocaltimeoffsetMinutes, this is no longer used or needed. The 'short' or 'long' no longer do anything but, keep "short", as it appears in the above call, the code will miss it if it is not there.

Now, about this server-php, etc. stuff. I don't know what it is for ssi, but if you use server-asp the file must be named filename.asp, if you use server-php the filename.php extension must be used. Filename can be anything, it is the extension that is important. To figure out which one works on your server, if you don't already know, you can do what I did, try each one out until one of them works. I can find out the extension for SSI, I just don't recall it off hand. PHP is the best bet. The file can appear as a normal html file in all other respects, it is just this code and the different extension that are required to make the script work.

lprag
12-22-2005, 11:47 AM
Hello John,

I am still having problems with the script.

My server uses php and I loaded it that way at:

http://mprpiano.com/test/index.php

As you can see, all I get is a blank current time and no date.

View the file as source code and you can see my comment lines on the error messages I get and other problems.

Hopefully you can copy the file and rewrite it so it works. Then email it back to me as an attachment and I'll try it again.

Again, thanks.

Lowell Prag
lprag@msen.com

jscheuer1
12-22-2005, 03:47 PM
I did just email you an attachment of my version. It looks like you over thought this one a bit, changing things in the code that weren't meant to be changed. For others, without the benefit of your inbox, here is my version (still not certain it shows the user's local time but, at least it works on a server with php):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<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, 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.serverdate=new Date(servertimestring)
this.localtime=new Date(new Date(this.serverdate.toGMTString()).toLocaleString())
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
this.container.innerHTML=this.localtime.toLocaleDateString()+'<br>'
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+="Current Time: "+formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm
setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
}

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
}
return (num<=9)? "0"+num : num//if this is minute or sec field
}

</script>
</head>
<body>
<span id="timecontainer"></span>

<script type="text/javascript">
new showLocalTime("timecontainer", "server-php", "short")
</script>
</body>
</html>

lprag
12-22-2005, 04:50 PM
Hello John,

Yes thanks, your new script works but ...

it shows my server's time which is in another state and it doesn't show my local time in Michigan.

Here it is:

http://mprpiano.com/test/index.php

Now, can you figure out how to script it so that it figures out what time zone the viewer is in and adjusts the server's time accordingly and when necessary, also adjusts the day and date?

BTW: I looked at some of your music www sites. Very nice. Are you interested in a really superb Steinway grand piano or know someone who is? I have one for sale that I completely rebuilt and refinished. It's a Model A, 6'-2" which is the best 6' grand that Steinway ever made. Follow the link under the picture at: http://mprpiano.com

Lowell

jscheuer1
12-22-2005, 05:16 PM
I was afraid of that, back to the drawing board. I'll let you know fairly soon what I come up with or if I am stumped.

jscheuer1
12-24-2005, 07:06 AM
Ok, this problem is actually much simpler than I realized at first. All that you need to do is use the original version of this Local Time script (http://www.dynamicdrive.com/dynamicindex6/localtime.htm) that we've been talking about. Adding just one variable and perhaps doing a little trial and error math will get you what you are looking for. Add this variable (highlighted red) near the top of the script:


/***********************************************
* 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"]

var localOffset=new Date().getTimezoneOffset()*-1

And use this method to call the script from the body:


Current Local Time: <span id="timecontainer"></span>

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

Now, in many cases your server time is already GMT or may be fetched that way by this script. If so, the local time displayed will be correct and you are done. If it is not, that is where a little trial and error comes in. Determine the hours difference between the time displayed and your true local time, multiply by 60 and either add or subtract it in the call. For example, let's say that the time displayed is 3 hours later than it should be. 3 times 60 is 180, so you need to subtract it from the local offset:


new showLocalTime("timecontainer", "server-php", [localOffset-180], "long")

That's it! Once you have the adjustment worked out for your locality it will be correct in any time zone. If you need help formatting the output (like before) once you have this working, let me know.

lprag
12-24-2005, 12:38 PM
Hello John,

Thanks. It appears that you may have solved the problem but I can't say for sure, as I don't know exactly where my server is.

When I ran the script with no changes, the time was 6 hours early. As my server is in the USA, I guess that was gmt time but I'm not sure. Anyhow, I added 360 to localOffset and it now gives the correct time in Michigan.

Here is the test page:

URL removed as requested.

Now, how can I tell if it will give the correct time for a viewer in say California, Alaska, or Russia, etc?

If you can definitively answer that, you have the best date-time clock script available!

Also, some minor tweaks:

I don't know how to change the format of the script without rendering it inoperable.

Here’s what it looks like now:

Current Local Time: Saturday, December 24, 2005 07:14:35

Here’s how I want it to look:

Saturday, December 24, 2005
Current time: 7:14:35 a.m.

Note that I removed the leading “0” in front of the hour and added the “a.m.” in lower case.

How do I change the script to achieve this?

Lowell

jscheuer1
12-24-2005, 02:53 PM
I tested this out on a friends server in CA. I am in PA and that worked. Also, your page shows the correct local time here in PA. That's the best 'proof' I can offer from here. Now about formatting, replace the showLocalTime.prototype.updateContainer function with this one:


showLocalTime.prototype.updateContainer=function(){
var thisobj=this
this.container.innerHTML=this.localtime.toLocaleDateString()+'<br>'
var hour=this.localtime.getHours()
var minutes=this.localtime.getMinutes()
var seconds=this.localtime.getSeconds()
var ampm=(hour>=12)? "p.m." : "a.m."
var dayofweek=weekdaystxt[this.localtime.getDay()]
this.container.innerHTML+="Current Time: "+formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm
setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
}

and change this (in the body):


Current Local Time: <span id="timecontainer"></span>

to this:


<span id="timecontainer"></span>

lprag
12-24-2005, 04:02 PM
Hello John,

Superb!!!

You can see the results here,
on my actual www home page:

URL removed as requested.

It's at the bottom:

"Thanks for visiting our www site:"
.........
.........

Now that you have this script, why not replace the other date-clock scripts with it, as it is now the most accurate way to do it or so we hope, until proven otherwise.

Again, many thanks and if you are ever in Detroit,
I'll be happy to buy you a beer!

Lowell

MoboMan
02-23-2006, 12:24 PM
Can I just jump in here please?

I have a related problem regarding displaying GMT/UTC on my aviation related website (which always times everything worldwide in GMT)?

I have run the scripts in this thread and get no response or display for either date or time just two lines like this:-
NaN
Current Time: Na: Na: Na AM

Using my PCs clock is OK but is not dynamic enough for me or a worldwide audience!!

Any assistance would be greatlfully received :)

My ISP here in the UK is www.ntlworld.com