1

Right, basically I have a countdown timer that doesn't do exactly what it says on the tin.

The PHP outputs correctly, but once it's echo'd within the Javascript, the date constantly changes.

I will load the page, and it will show one ETA, and I'll refresh and it could jump forward an hour or jump back several hours?

I just need the timer to countdown to the 20th March 2015.

The PHP Code

<?php 
  $futureDate = '2015-03-20';
  $d = new DateTime($futureDate);
?>

The Javascript

    $('#counter').countdown({

        timestamp : (new Date()).getTime() + <?php echo $d->diff(new DateTime())->format('%a*%h*%i*%s*1000'); ?> 
});

Here's a Live Preview

Preview

Please, somebody help lol

DJ Burb
  • 2,346
  • 2
  • 29
  • 38
Jake Duncan
  • 71
  • 1
  • 8
  • use `getDays` instead of `getTime()`, since you're converting to days in microseconds. – Mouser Feb 25 '15 at 21:49
  • Wouldn't it be `futureDate - presentDate` ? Or just `futureDate`, without adding anything, depending on what you do with it? – blex Feb 25 '15 at 21:49
  • 1
    What's the significance of all this math? `timestamp: (new Date()).getTime() + 22*2*10*55*1000` – Marc Feb 25 '15 at 21:53

2 Answers2

0

you can use something like this in javascript

how to countdown to a date

var end = new Date('03/20/2015 0:0 AM');

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function showRemaining() {
    var now = new Date();
    var distance = end - now;
    if (distance < 0) {

        clearInterval(timer);
        document.getElementById('counter').innerHTML = 'EXPIRED!';

        return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
    var minutes = Math.floor((distance % _hour) / _minute);
    var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById('counter').innerHTML = days + 'days ';
    document.getElementById('counter').innerHTML += hours + 'hrs ';
    document.getElementById('counter').innerHTML += minutes + 'mins ';
    document.getElementById('counter').innerHTML += seconds + 'secs';
}

timer = setInterval(showRemaining, 1000);
Community
  • 1
  • 1
ETS
  • 516
  • 2
  • 7
  • 15
0

Replace your javascript with

$('#counter').countdown({
        timestamp : new Date('2015/03/20') 
});

As it is now you're overcomplicating things - you just need to set the date of the countdown to when you want it to finish.

Zach Sadler
  • 570
  • 3
  • 11