Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Regarding date-time clock ...

  1. #1
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Regarding date-time clock ...

    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.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

  3. #3
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by lprag; 12-20-2005 at 04:35 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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:

    Code:
    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.
    Last edited by jscheuer1; 12-20-2005 at 07:25 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    For that, you can modify the function showLocalTime.prototype.updateContainer, replace it with this one:

    Code:
    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:

    HTML Code:
    <span id="timecontainer"></span>
    and below that, have the short script call (unchanged from the previous mod):

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <!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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •