www.webdeveloper.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38

Thread: Servertime Count problem

  1. #16
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    I only use Opera, so it works in that too...

    What errors? I don't get any.

    Upload this to your server and post the link here.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body><div id="clock"><script>/*
    Website clock that displays the server time on the local users machine. Requires PHP to set the time string to RFC 2822 format.
    
    Script as seen on Webdeveloper.com is supplied as is without warranty, etc, so your on your todd. Please tell a friend.
    
    */
    difference = new Date("<?php echo date('r'); ?>") - new Date().getTime(); // get the time difference
    clock = {
    	cycles:0,
    	update:function(){
    		servertimeObject = new Date( (new Date().toGMTString()) ).getTime() + difference;
    		clock.currentTime = new Date( servertimeObject ).toGMTString(); // outputs format hh:mm:ss
    		clock.obj.innerText = clock.currentTime.slice(16,25) + " Hrs"; // update with the string
    	},
    	init:function(){
    		if( ++clock.cycles > 99 || clock.obj ) clearInterval( clock.auto );
    		if( !clock.obj ){ 
    			try{ clock.obj = document.getElementById("clock"); }catch(e){ delete e; }
    			}
    		if( clock.obj ){ // if we have the object, set an interval timer to run
    			clock.auto = setInterval("clock.update()",1000);
    			clock.obj.title = "Clock";
    			}
    	},
    	auto:setInterval("clock.init()",1000)
    }
    </script></div>
    </body>
    </html>

  2. #17
    Join Date
    Oct 2011
    Posts
    11

    ..

    With errors I mean what problems my "clock" is running into..

    I uploaded it to this site:
    http://battelamsp.comyr.com/text.php

    I'm just used to use .php extension :P
    The project is gonna be a simple RPG game :P
    Don't mind the layout of the mains site :P
    Just had to make something (badly written) to see where I was gonna place everything..

  3. #18
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    Hey guys, in case anyone was wondering: I made the code I posted quickly; I excluded GMT/UTC on purpose, as I thought we needed a relationship between the server's local time and the client's local time? Correct me if I'm wrong, but that only needs 1 offset, I would think; completely ignoring daylight savings time etc.
    Computer programmer / web developer.

  4. #19
    Join Date
    Oct 2011
    Posts
    11
    Not that good in english,, what do you actually mean? And what do you mean with daylight savingstime?

  5. #20
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by FireVirus View Post
    With errors I mean what problems my "clock" is running into..

    I uploaded it to this site:
    http://battelamsp.comyr.com/text.php

    I'm just used to use .php extension :P
    The project is gonna be a simple RPG game :P
    Don't mind the layout of the mains site :P
    Just had to make something (badly written) to see where I was gonna place everything..
    Your link is dead

  6. #21
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by FireVirus View Post
    With errors I mean what problems my "clock" is running into..

    I uploaded it to this site:
    http://battelamsp.comyr.com/text.php

    I'm just used to use .php extension :P
    The project is gonna be a simple RPG game :P
    Don't mind the layout of the mains site :P
    Just had to make something (badly written) to see where I was gonna place everything..
    I meant upload my clock version, I see a 1 hour difference between my local machine in DLS and the server which is UTC and the only way to tell if this works is to have the script on a remote server as the server my hosting company services me with is actually half a mile down the road from me, so any time difference is minimal albeit for the obvious difference of one hour. The acid test is to have it in the open on another domain.

  7. #22
    Join Date
    Oct 2011
    Posts
    11

    oops

    Sorry, my mistake:
    http://battlemaps.comyr.com/text.php

    It's located in the US. Somewhere.. It's from **********.

  8. #23
    Join Date
    Oct 2011
    Posts
    11

    Jezus -.-

    Now I'm at school,, testing the clock like I had it, but discovered another problem..

    I don;t want to force my users to use the NEWEST version of IE, FF or Chrome, but in IE8 (school) the clock is bugged with the hours, which affects the minutes :S

    I would like to use the toGTMstring etc.. but Gotta get it work for FF & Chrome then.. :\
    Could you help with this?

    Edit:
    Your code doesn't work in IE8 on my school as well.. it says: This property isn't supported by the object.
    Really don;t know what to do :S
    Adjust this method for working in all major browsers / common used browsers.. or just try to fix my script? :S
    Last edited by FireVirus; 10-24-2011 at 02:31 AM. Reason: See Edit

  9. #24
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    ECMA script is supported by all the browsers you mention, so why your having problems is likely to be mainly because your clients are using out dated software.

    I got it working in MSIE8, it appears that it does not like the word clock, when I changed it to xclock and all other references in the script, under MSIE8 it worked.

    As for browsers, EVERYONE who uses the web should be using up to date browsers where possible and not just for the benefit of the vendors of software or developers. Latest releases equal security patches to threats that are very real as well as the more functional aspects like web scripts.

  10. #25
    Join Date
    Oct 2011
    Posts
    11
    I don't want to force them, some people might like IE8 more, or don't know how to update,, like older ppl.

    I guess I've found some script which might work well.. ;D
    Will show you later,,, got a lot to do right now :S (first gonna go sleeping :S)

  11. #26
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    A simple script to understand what we do...
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Server local time</title>
    <style type="text/css">
    p {margin:5px;}
    </style>
    </head>
    <body>
    	<p><b>At the opening of this page</b></p>
    	<p><b>Server</b> (PHP)
    	<br>The server UTC time was <span id="srvUTC">[loading]</span>
    	<br>The server local time was <span id="srvLcl">[loading]</span>
    	<br><b>Client</b> (javascript)
    	<br>The client UTC time was <span id="cltUTC">[loading]</span>
    	<br>The client local time was <span id="cltLcl">[loading]</span>
    
    	<br><b>Differences</b> (seconds)
    	<br>The UTC difference (server-client) : <span id="difUTC"></span>
    	<br>The server difference (local-UTC) : <span id="difSrv"></span></p>
    	<p><br><b>Now the local server time is </b><span id="clock">[loading]</span></p>
    
    <script language="javascript" type="text/javascript">
    
    <?php
    // Server UTC and local dates to javascript variables
    	$srcUTCTm = gmdate('H:i:s');echo "var srvUTCStr ='".($srcUTCTm)."';\n";
    	$srcLclTm = date('H:i:s');echo "var srvLclStr ='".($srcLclTm)."';\n";	
    ?>
    
    // Display Server variables
    	document.getElementById("srvUTC").innerHTML = srvUTCStr;
    	document.getElementById("srvLcl").innerHTML = srvLclStr;
    
    // Number to string with leading Zeros 
    Number.prototype.wthLdz=function(){
    	var w=this.toString();while(w.length<2)w='0'+w;return w;
    }
    // Local Date String hh:mm:ss 
    // (searching /\d\d:\d\d:\d\d/ in toString() or to localeSring() could be an other way) 
    Date.prototype.toLclStr=function(){
    	return this.getHours().wthLdz()+':'+this.getMinutes().wthLdz()+':'+this.getSeconds().wthLdz();
    }
    // Client UTC 
    	tdy = new Date();var cltUTCStr = tdy.toUTCString().slice(17,25);// Thanks JunkMale
    	document.getElementById("cltUTC").innerHTML = cltUTCStr;
    	document.getElementById("cltLcl").innerHTML = tdy.toLclStr();
    
    // Date String to seconds
    String.prototype.hmsToSec=function(s){
    	var t=(':'+this).replace(/:0(\d)/g,':$1').substr(1).split(/:/g);
    //	alert(parseInt(t[0])+" "+parseInt(t[1])+" "+parseInt(t[2])) 
    	return (parseInt(t[0])*60+parseInt(t[1]))*60+parseInt(t[2]);
    }
    // Difference in seconds
    var dltUTC = srvUTCStr.hmsToSec()-cltUTCStr.hmsToSec();
    document.getElementById("difUTC").innerHTML = dltUTC; 
    var dltSrv = srvLclStr.hmsToSec()-srvUTCStr.hmsToSec();
    document.getElementById("difSrv").innerHTML = dltSrv;
    
    
    // the clock
    
    var scdSrv = cltUTCStr.hmsToSec() // client UTC
    			- (-dltUTC)   // server UTC
    			- (-dltSrv);  // server locale
    function dspClc(){++scdSrv;
    	var m=Math.floor(scdSrv/60),h=Math.floor(m/60),s=scdSrv&#37;60; 
    	h=h%24;m=m%60;                                             
    	document.getElementById('clock').innerHTML=h.wthLdz()+':'+m.wthLdz()+':'+s.wthLdz();
    }
    setInterval(dspClc,1000);
    
    </script> 	 
    </body>
    </html>
    We have to take care there is so many Universal Time that servers or machines...
    Last edited by 007Julien; 10-25-2011 at 08:58 AM. Reason: error and complements

  12. #27
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    We have to take care there is so many Universal Time that servers or machines...
    Erm... UTC is "Coordinated Universal Time" and in some respects we have to be careful because some home hosting people do not appreciate the importance of accurate time. Lets face it, if the clocks that drive the SATNAV satellites are out by a second, this translates to cars driving off cliffs and ploughed in to shop because users obey the bloody things. So if your server time is wrong, you have no way of knowing just when something happened on your server.

    Any time differences on servers that do not appear to relate to the time zone that they are in is just very sloppy administration and if the company hosting the rackspace were ever to produce "Evidence" from their systems to a court, they wouldn't be allowed to submit the evidence because the evidence and the companies credibility would be compromised.

    All hosting companies of worth synchronize server clocks... those that do not are playing with fire and if you discover your hosts computer not registering the correct time, you should point this out to them. This assumes that you know the physical location of the servers timezone.

    GMT is the time reference which denotes ALL the worlds clocks to be set by despite the time for Europe originates or did originate in Germany, its synchronized with GMT.

    UTC is used in business because it does not have DSL issues, it is separate from GMT as far as it ticks at a different rate but will always be an hour different from GMT when the local machine is observing DSL and machines that observe DSL can be easily tested for. Subtracting GMT from UTC time and will return a zero or number less than zero which indicates DSL. Easy true false calculation...


    Code:
    dObj = new Date();
    dslChk = (dObj.getUTCHours() - dObj.getHours());
    if(dslChk) alert("Day Light Savings is observed on this machine at the moment");
    toLocalString() only returns a date and a time, no zone information.

    getTimezoneOffset() returns the difference in minutes ahead or behind UTC in minute. The value is positive when the local timezone is behind UTC and negative when it is ahead.

    Also, please note that DSL and timing scripts, you do not account for DSL, thats already been calculated by the local operating system and in case you don't know what to believe, call Microsoft and they will tell you that Javascript gets it time from the operating system, not BIOS and DSL is calculated locally within the operating system from the users locality and country settings.


    @007Julien --
    Code:
    var cltUTCStr = tdy.toUTCString().slice(17,25);// Thanks JunkMale
    -- NP

    and when you discover that parseInt is pointless, you can simply subtract zero from a numerical string value to get a number, you will wonder why we have parseInt.

    Code:
    var somevalue = "27";
    alert( typeof somevalue);
    somevalue -= 0;
    alert( typeof somevalue);
    returns 27 as a number and not a string.

  13. #28
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    @JunkMale
    Your test for daylight saving time is wrong !
    The difference dslChk = (dObj.getUTCHours() - dObj.getHours()); give the time zone with or without DST !
    GMT (for Greenwich Mean Time) is an old depreciated English (or British) term. UTC (for Coordinated Universal Time) is the international term...

  14. #29
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by 007Julien View Post
    @JunkMale
    Your test for daylight saving time is wrong !
    The difference dslChk = (dObj.getUTCHours() - dObj.getHours()); give the time zone with or without DST !
    GMT (for Greenwich Mean Time) is an old depreciated English (or British) term. UTC (for Coordinated Universal Time) is the international term...
    I don't quite know how you figure that one out.

    UTC time originate at Longditude of Zero Degrees which is the Grenwich Meridian.

    UTC time does not observe the DSL movements in time, its tick rate takes in to account the various leap seconds to keep time so you don't end up with a leap year.

    Therefore getting the hours on the users local machine with getHours() and subtracting it from UTC will return Zero if that users machine is not observing DSL at that moment in time and -1 if that users machine is 1 Hour ahead. Remember, all DSL movements are forward in time and return to UTC time when not observing DSL.

    Care to elaborate on your original statement? The code I posted is very commonly found all over the internet and your post would leave the impression that it in itself is wrong when it is not.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  15. #30
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    For example, I am in DSL at the moment. When DSL ends I will post another.
    Attached Images Attached Images
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles