www.webdeveloper.com
Results 1 to 8 of 8

Thread: 24 Hour JS Countdown via server-time. Possible?

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    11

    24 Hour JS Countdown via server-time. Possible?

    Hi,

    I'm looking for some help with a javascript/php code that will display a countdown from midnight server time to midnight servertime the next day. and reset its self everyday.

    So far i've got this:
    Code:
    function ShowTimes() {
      var now = new Date();
      var hrs = 23-now.getHours();
      if (hrs < 10) hrs = '0'+hrs;
      var mins = 59-now.getMinutes();
      if (mins < 10) mins = '0'+mins;
      var secs = 59-now.getSeconds();
      if (secs < 10) secs = '0'+secs;
      document.getElementById('countdownToMidnight').innerHTML = hrs+':'+mins+':'+secs+'';
    }
    var _cntDown;
    function StopTimes() {
    	clearInterval(_cntDown);
    }
    But obviously this does it via the users computer time so is different depending where you are.

    I need it so wherever you are in the world the countdown is at the same time as everyday at midnight (servertime) an image on the website changes.


    Sorry if I've done something wrong here, it's my first post.
    Any help would be very much appreciated and if you require any more info just let me know

    Thanks
    - Jamie

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    See Date.UTC() and getTimezoneOffset() to work with UTC time !
    A good script about time Zones : jsTimezoneDetect

  3. #3
    Join Date
    Jan 2013
    Posts
    11
    Hi
    Thanks although I'm having trouble implementing this into my code.
    what would i set the UTC date to and how would i use that to get the countdown correct?

    Sorry for being a bit simple, im still fairly new to JS.

    thanks

  4. #4
    Join Date
    Nov 2010
    Posts
    1,085
    hmmm... but I believe that using UTC will give you the UTC time according to the user's computer.

    So if I set my computer to 8:30am, January 4 1982 the code will simply translate that time to UTC.

    or no?

    To get the server time you have to get it from the server, which is not an easy thing with javascript, but is trivial with a server side language.

    Using only javascript, you can do it via an ajax call, using the date property of the response header. See this thread (and my warning about toLocaleString)

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    If your clock is right your UTC time is the same as the UTC (or GMT) server time !
    Then the time zone of the server is enough with something like this :

    Code:
    var dlt="<?php echo date('Z'); ?>";// Timezone offset in seconds. The offset west of UTC is negative, and the offset east of UTC is positive (-43200 to 43200).
    to determine midday on the server !

    See w3scholls.com PHP Date page
    Last edited by 007Julien; 01-03-2013 at 06:09 PM.

  6. #6
    Join Date
    Nov 2010
    Posts
    1,085
    Quote Originally Posted by 007Julien View Post
    If your clock is right
    That's the big IF - I guess it depends on how important accuracy is to the app

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    It's easy to synchronize a clock with a time server. Since it's always possible to make control with UTC time (with the limitation of the propagation time) and specify the server time prevails...
    Last edited by 007Julien; 01-03-2013 at 06:31 PM.

  8. #8
    Join Date
    Nov 2010
    Posts
    1,085
    Quote Originally Posted by 007Julien View Post
    It's easy to synchronize a clock with a time server.
    I think you may be overestimating your average internet user there

Thread Information

Users Browsing this Thread

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

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