Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts

    Countdown based on server's time

    Hey.

    I'm currently looking for a countdown script, which is based on the server's time. I've only found scripts, that's based on the local computer's time, which means the output of the script changes, when the time on the computer is changed.

    And I want to make a public announcement which counts down to something - and should output the same at every person.

    I'm not that familiar with JavaScript, I know some jQuery - if anyone can supply any links or can help me with this, I'd appreciate it.
    Last edited by [vengeance]; 12-01-2009 at 10:29 PM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by [vengeance] View Post
    Hey.

    I'm currently looking for a countdown script, which is based on the server's time. I've only found scripts, that's based on the local computer's time, which means the output of the script changes, when the time on the computer is changed.

    And I want to make a public announcement which counts down to something - and should output the same at every person.

    I'm not that familiar with JavaScript, I know some jQuery - if anyone can supply any links or can help me with this, I'd appreciate it.
    what's the problem with gmt?

    best regards

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by oesxyl View Post
    what's the problem with gmt?

    best regards
    Sorry, but I didn't mention anything regarding GMT time?
    My server runs on GMT +1 by default. And I want a count down script that relies on the server's time itself, not the local computer.

    For example, if the time's 1:30 then the countdown script would output something. If I change the time back to 1:15 the countdown script would output something else... a bigger number because we've "gone back in time".

    Hope I made that clearer this time.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by oesxyl View Post
    what's the problem with gmt?

    best regards
    UTC time is universal time (formerly GMT).

    Here is an example which will show the PST time (GMT-8) in all browsers regardless of the user's local time.
    You would wish to use GMT+1.

    Code:
    <script type = "text/javascript">
    
    var dateobj = new Date();
    var hrs  = dateobj.getUTCHours()-8;  // offset GMT - PST
    if (hrs < 0) {hrs = hrs + 24}
    var mins = dateobj.getUTCMinutes();
    if (mins <10) {mins = "0" + mins}
    var secs = dateobj.getUTCSeconds();
    if (secs <10) {secs = "0" + secs}
    var currenttime = hrs+":"+mins+":"+secs; //create time string
    alert (currenttime);
    
    </script>

    If you want some artificial time that you can alter in some way then you have no alternative but to user server-side scripting. Javascript is a client-side language and cannot interact with the server.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by [vengeance] View Post
    Sorry, but I didn't mention anything regarding GMT time?
    no you didn't mention, I mention,
    I apologise that was not clear, why you don't use gmt?

    My server runs on GMT +1 by default. And I want a count down script that relies on the server's time itself, not the local computer.

    For example, if the time's 1:30 then the countdown script would output something. If I change the time back to 1:15 the countdown script would output something else... a bigger number because we've "gone back in time".

    Hope I made that clearer this time.
    yes is more clear but I have same question,
    you want to countdown the time from the moment when a visitor request a page to a given time moment. If you use gmt doesn't matter the local time of the visitor.
    Let's say you want to stop at 3:00 gmt. If a user request the page at 1:30 gmt, will countdown for 1 hour and 30 min but if user time is 1:15 gmt will be 1 hour and 45 min.
    Doesn't matter local time of the visitor or server,

    best regards

  • #6
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by oesxyl View Post
    no you didn't mention, I mention,
    I apologise that was not clear, why you don't use gmt?


    yes is more clear but I have same question,
    you want to countdown the time from the moment when a visitor request a page to a given time moment. If you use gmt doesn't matter the local time of the visitor.
    Let's say you want to stop at 3:00 gmt. If a user request the page at 1:30 gmt, will countdown for 1 hour and 30 min but if user time is 1:15 gmt will be 1 hour and 45 min.
    Doesn't matter local time of the visitor or server,

    best regards
    I see. Do you have any examples of how I could achieve this? Afterall, I have barely any knowledge of JavaScript.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by [vengeance] View Post
    I see. Do you have any examples of how I could achieve this? Afterall, I have barely any knowledge of JavaScript.
    ok, I will prepare an example.

    best regards

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by [vengeance] View Post
    I see. Do you have any examples of how I could achieve this? Afterall, I have barely any knowledge of JavaScript.
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Countdown timer test</title>
        <script type="text/javascript" src="test.js"></script>
      </head>
      <body>
        <p id="msgid"></p>
      </body>
    </html>
    Code:
    /* -*- c++ -*- */
    var evdatetime = '12/01/2009 19:21 GMT';
    var evdt = new Date(evdatetime);
    var tdiff = 10000;
    
    function diff2int(){
      var now = new Date();
      return Math.floor((evdt.getTime() - now.getTime())/1000);
    }
    
    function diff2str(){
      tdiff = diff2int();
      var diff = tdiff;
      var s = diff % 60;
      diff = (diff - s)/60;
      var m = diff % 60;
      diff = (diff - m)/60;
      var z = diff % 24;
      diff = (diff - z)/24;
      return (diff + ':' + z + ':' + m + ':' + s);
    }
    
    function makeitmove(){
      var txtcnt = document.getElementById('msgid');
      var txtmsg = ['Time until I go: ','Ok, I&apos;m out'];
      var now = new Date();
      if(txtcnt){
        if(tdiff > 0){
          txtcnt.innerHTML = txtmsg[0] + diff2str();
          txtcnt.innerHTML += '<br />' + now.toUTCString();
          txtcnt.innerHTML += '<br />' + evdt.toUTCString();
        }else{
          txtcnt.innerHTML = txtmsg[1];
        }
      }
    }
    
    window.onload = function(){
      setInterval(makeitmove,1000);
    };
    best regards

  • Users who have thanked oesxyl for this post:

    [vengeance] (12-01-2009)

  • #9
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    @oesxyl: Thank you so much for taking your time to write this! It works great and easy for me to understand aswell.

    Kudos to you!


  •  

    Posting Permissions

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