SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Countdown againist server time?

    Hi, is it possible to have javascript to start counting down based on my given timestamp against my server time instead of client's time? The following is code:

    PHP Code:

    var eventdate = new DateMY TIME VARIABLE );

    function 
    toSt(n)
    {
        
    s=""
        
    if(n<10s+="0"
        
    return s+n.toString( );
    }
     
    function 
    countdown( )
    {
        
    = new Date( );
        
    count Math.floor((eventdate.getTime()-d.getTime())/1000);
        
        if( 
    count <= )
        {
            $(
    'days').innerHTML  "----";
            $(
    'hours').innerHTML "--";
            $(
    'mins').innerHTML  "--";
            $(
    'secs').innerHTML  "--";
            return;
        }
        
        $(
    'secs').innerHTML toSt(count%60);
        
    count Math.floor(count/60);
        $(
    'mins').innerHTML toSt(count%60);
        
    count Math.floor(count/60);
        $(
    'hours').innerHTML toSt(count%24);
        
    count Math.floor(count/24);
        $(
    'days').innerHTML count;
      
        
    setTimeout("countdown()",500);


  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Well assuming the date and time on the client computer is correct (slight possibility) then either use the UTC versions of the get and set functions to retrieve and set Universal time or use the timezoneoffset field to determine the offset from Universal time. You then further adjust that for the time difference between your server time and the time in Greenwich, England.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •