Search code examples
phpjavascriptjquerycountdown

Trying to use jQuery Countdown with UNIX timestamp from php


As the title says I'm facing a problem displaying the right left time to an end date.

I'm using this jquery plugin: http://keith-wood.name/countdown.html

Let's assume that our timestamp is generated by a PHP script like this: $end_date = strtotime($row4['end']); // 2012-05-09 06:00:00 becomes 1336536000

I use this ($i is used inside a loop):

$('.count-<?php echo $i; ?>').countdown({until: new Date(<?php echo $end_date; ?> * 1000), layout: '{dn} days {hnn} hrs {mnn} min {snn} sec', compact: true});

Right now it's 06:21 AM here. Instead of the expected 1 day left result I get "1 days 17 hrs 04 min 21 sec".

What am I missing here?


Solution

  • I have been using the API for quite a while now, but I cann't seem to figure out what's up with your code. Perhaps (but just perhaps) you're using the untill property wrong.

    My code that I usually use:

    $(function(){
        var countdown = $('#countdown'),
            ts = new Date(<?php echo $date_to * 1000; ?>),
            finished = true;
    
        if((new Date()) > ts)
        {
            finished = false;
        }
    
        $('#cool-countdown').countdown({
            timestamp   : ts,
            callback    : function(days, hours, minutes, seconds)
            {
                var message = "";
    
                message += days + " days, ";
                message += hours + " hours, ";
                message += minutes + " minutes, ";
                message += seconds + " seconds ";
    
                message = (finished ? "Countdown finished" : "left untill the New Year");
    
                countdown.html(message);
            }
        });
    
    });
    

    Obviously you should extend this to feature singular.