SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    countdown timer woes

    hello

    im trying to compare two times and initiate a countdown.

    I can handle the php aspect fine, and can format the date into whatever format it needs to be - the problem comes about when I try and plug my date into javascript.

    Code:
    <script language="JavaScript">
    TargetDate = "12/31/2020 5:00 AM";
    BackColor = "palegreen";
    ForeColor = "navy";
    CountActive = true;
    CountStepper = -1;
    LeadingZero = true;
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    FinishMessage = "It is finally here!";
    </script>
    
    function calcage(secs, num1, num2) {
      s = ((Math.floor(secs/num1))%num2).toString();
      if (LeadingZero && s.length < 2)
        s = "0" + s;
      return "<b>" + s + "</b>";
    }
    
    function CountBack(secs) {
      if (secs < 0) {
        document.getElementById("cntdwn").innerHTML = FinishMessage;
        return;
      }
      DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
      DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
      DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
      DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
    
      document.getElementById("cntdwn").innerHTML = DisplayStr;
      if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
    }
    
    function putspan(backcolor, forecolor) {
     document.write("<span id='cntdwn' style='background-color:" + backcolor + 
                    "; color:" + forecolor + "'></span>");
    }
    
    CountStepper = Math.ceil(CountStepper);
    if (CountStepper == 0)
      CountActive = false;
    var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
    putspan(BackColor, ForeColor);
    
    // THIS IS THE RELEVANT PART ( I THINK)
    var dthen = new Date(TargetDate);
    var dnow = new Date();
    //var dnow = new Date(<?php echo $start_date; ?>);
    // END RELEVANT PART
    
    if(CountStepper>0)
      ddiff = new Date(dnow-dthen);
    else
      ddiff = new Date(dthen-dnow);
    gsecs = Math.floor(ddiff.valueOf()/1000);
    CountBack(gsecs);
    so it seems that var dnow = new Date(); reads the users system time and sets the countdown based on that - I however need to use my server time which I grab with php.
    How can I substitute the new Date(); with the php (as tried in the sample), but make it work? at present it is way, way, way off in the calculaton presumably because of the start date format.

    I have tried:

    var dnow = '11 27 2006 21:25:33';

    and also

    var dnow = '11/27/2006 9:20 PM';
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Based on the code you've presented, I would assume this to work:

    Code:
    var dnow = new Date('11/27/2006 9:20 PM')
    So make your PHP generated date string the argument for the javascript Date() constructor. Don't forget the quotes as you did in your commented out attempt.

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thankyou that did the trick, I spent alot of time stuffing around to realise that my php variable was not reaching the included JS file.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


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
  •