www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 04-13-2010, 03:37 PM
    covana covana is offline
    Registered User
     
    Join Date: Apr 2010
    Posts: 3
    Count down script time inaccuracy

    I have a simple countdown script that takes an parameter for the expiration date and creates a count down from now until the expiration. Everything is handled using unix timestamps.

    here is the javascript

    Code:
    function countdown(exp_time){
    	var now = Math.round(new Date().getTime() / 1000);
    	//alert(now);
    	var time_til = exp_time - now;
    	if(time_til > 0){
    	
    		days = Math.floor(time_til/(60*60*24));
    		
    		time_til %= (60*60*24);
    		
    		hours = Math.floor(time_til/(60*60));
    		
    		time_til %= (60*60);
    		
    		minutes = Math.floor(time_til/60);
    		
    		time_til %= 60;
    		
    		seconds = time_til;
    		
    	document.getElementById('count_update').innerHTML = days +':'+hours+':'+minutes+':'+seconds;
    	
    		setTimeout('countdown('+exp_time+');', 1000);
    	}
    	else{
    		document.getElementById('count_update').innerHTML = 'No Time Remaining!';
    	}
    }
    the parameter is passed in using php like this

    Code:
    $expired = mktime(0,0,0,MM,DD,YYY);
    
    
    <div id='count_update'></div>
    <script type='text/javascript'>countdown(<?php echo $expired]; ?>)</script>
    Where MM, DD, YYYY in the mktime function are whatever is pulled from the database.

    The script seems to work fine except for one thing. The time shown on the countdown does not expire at 12am on the servers clock. Its anywhere from 1 to 4 hours off depending on the server it is running on. Am I over looking something that I should be adding in the js to make the clock accurrate?

    Thanks alot for any help

    -Kyle
    Reply With Quote
      #2  
    Old 04-13-2010, 04:27 PM
    BIOSTALL BIOSTALL is offline
    Registered User
     
    Join Date: Oct 2005
    Location: Cambridgeshire, UK
    Posts: 191
    After playing with this for a while it appears GMT has something to do with the offset, hence it's always exactly 1 hour out.

    Try changing:

    HTML Code:
    var now = Math.round(new Date().getTime() / 1000);
    To:

    HTML Code:
    var now = new Date();
    var offset = now.getTimezoneOffset();
    	
    var now = Math.round(new Date().getTime()/1000)-(offset*60);
    That will get the difference in the timezone and factor that into the final calculation.

    Hope that helps somewhat
    __________________
    BIOSTALL.com
    - Freelancing Services
    - Web Development Snippets, Hints and Tips
    Reply With Quote
      #3  
    Old 04-13-2010, 04:28 PM
    BIOSTALL BIOSTALL is offline
    Registered User
     
    Join Date: Oct 2005
    Location: Cambridgeshire, UK
    Posts: 191
    Please note, your code will also currently generate a PHP parse error due to the following bit of code:

    PHP Code:
    <?php echo $expired]; ?>
    Just remove the closing square bracket.
    __________________
    BIOSTALL.com
    - Freelancing Services
    - Web Development Snippets, Hints and Tips
    Reply With Quote
      #4  
    Old 04-13-2010, 04:41 PM
    covana covana is offline
    Registered User
     
    Join Date: Apr 2010
    Posts: 3
    Quote:
    Originally Posted by BIOSTALL View Post
    Please note, your code will also currently generate a PHP parse error due to the following bit of code:

    PHP Code:
    <?php echo $expired]; ?>
    Just remove the closing square bracket.

    Thanks a lot. I believe you additions have made it more accurate. The parse error was just in the sample code there. I am actually pulling the expiration date from a stored value in the database. I just forgot to remove the ] when editing the code to post here as an example.

    Thanks again
    Reply With Quote
      #5  
    Old 04-13-2010, 06:00 PM
    covana covana is offline
    Registered User
     
    Join Date: Apr 2010
    Posts: 3
    Still not sure if it is exactly right. Should I be accounting for time zone offsets when i store the expiration date in the database and not just use mktime() alone?
    Reply With Quote
      #6  
    Old 04-13-2010, 06:04 PM
    BIOSTALL BIOSTALL is offline
    Registered User
     
    Join Date: Oct 2005
    Location: Cambridgeshire, UK
    Posts: 191
    You should store the date/time generated by PHP (ie. the server time) rather than what is generated by Javascript (ie. the time of the users computer).
    __________________
    BIOSTALL.com
    - Freelancing Services
    - Web Development Snippets, Hints and Tips
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 06:04 AM.


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.