Search code examples
phpjquerymysqlcountdown

Keith woods countdown timer DATETIME format Jquery


I have been playing with this for far too long now and thought I had better ask for some help as I really need to move on :) I'm very new to jquery

I have the Keith Wood's countdown timer displaying but, the time is wrong, the future date is 3 days in the future but, I'm getting 217224 days. The date is mysql DATETIME i.e; 2012-11-15 07:00:30

http://keith-wood.name/countdown.html

I only need to display the Days, Hours, Minutes, Seconds but, if I remove the Y, m nothing displays. Here's my code, I would be very great full of any help!

$(function () {
    var austDay = new Date(<?php
    $time = strtotime($row['auto_accept']);
    echo date('Y', $time);
    echo date('m', $time);
    echo date('d', $time-1);
    echo date('H', $time);
    echo date('i', $time);
    echo date('s', $time);?>);
    $('#countdown').countdown({until: austDay, format: 'dHMS'});
});

OK I seem to have it working using:

$(function () {
var austDay = new Date(<?php
    $endtime = strtotime($row['auto_accept']);
    echo date('Y,m,', $endtime);
    echo (date('j', $endtime)-30);
    echo date(',h,i,s', $endtime);
?>);

$('#countdown').countdown({until: austDay, format: 'dHMS'});
});

I just need to optimise the code a little better as Tomalak said, I'll check out the manual ;)


Solution

  • First, read up on how to use PHP's date function and figure out how to get the date in one shot. This would take no more than 10 or 15 minutes of your time and will quickly show you why you don't need to call date so many times (and save you time in the future and show you how I did it below).

    Second, it would probably be best if you used an Ajax call to get values from PHP into Javascript. The method below works but it's fugly.

    Your PHP:

    $timeToConvert = strtotime($row['auto_accept'] . ' -1 day');
    

    Your JS:

    var austDay = new Date(<?php echo $timeToConvert * 1000 ?>);
    $('#countdown').countdown({until: austDay});
    

    That should be all you need (I did not test this to make sure but it looks pretty simple so I expect this will work with the plugin you are using.)