0
votes

I have a php date, and i want the difference about php date and today's date. The code is: function countdown(counter, year, month, day, hour, minute, second){
var datePhp = new Date(year, month, day, hour, minute, second); var today=new Date(); var days=0; var hours=0; var minutes=0; var seconds=0; var sumeDays = 0;

    if (datePhp>today){
        var difference=(datePhp.getTime()-today.getTime())/1000;
        days=Math.floor(difference/86400);
        difference=difference-(86400*days);
        if(days==1){
            sumeDays=days*24;
        }
        hours=Math.floor(difference/3600);
        printHours=Math.floor(difference/3600)+sumeDays;
        difference=difference-(3600*hours);
        minutes=Math.floor(difference/60);
        difference=difference-(60*minutes);
        seconds=Math.floor(difference);`
            $("#counter" + counter).html(printHours + ' Hours ' + minutes + '                 Minutes ' + seconds + ' Seconds');

        if (days>0 || hours>0 || minutes>0 || seconds>0){
            setTimeout("countdown("+counter+", "+year+","+month+","+day+","+hour+","+minute+","+second+")",1000)
        }

The code runs, but runs wrong, because always say less than 24 hours. For example, if phpDate is 4/06/2014 09:30 (where 4 is the European day and 6 is June) and today is 2/06/2014 09:30 (where 2 is the European day and 6 is June). The code say that there are only 24 hours to phpDate.

And the code in html is:

  <div id='counter<?php echo $i ?>'></div>
  <script type="text/javascript">
  countdown(<?php echo $i?>, <?php echo $year?>, <?php echo $month?>, <?php echo $day?>, <?    php echo $hour?>, <?php echo $minute?>, <?php echo $second?>);
  </script> 
1
Where are year, month, day, hour, minute and second defined?bcmcfc
year, month, day, hour, minute and second are defined in php code, and i passed them by a function. Values about var are ok.mjosee7
@mjosee7 please add the output of console.log(new Date()) console.log(datePhp) to your question, this will help to see the problem, as it may e.g. be a timezone problem.t.niese

1 Answers

-1
votes
p = new Date('4/06/2014 09:30')
>Sun Apr 06 2014 09:30:00 GMT+0530 (India Standard Time)
n = new Date('2/06/2014 09:30')
>Thu Feb 06 2014 09:30:00 GMT+0530 (India Standard Time)
diff = p.getTime() - n.getTime()
>5097600000
diffHours = diff / (1000 * 60 * 60)
>1416

looks ok for me, 1416 hours from feb 06 to apr 06

I dont understand your code completely, but when i do it with below code I am getting 48 hours which looks good

var datePhp = new Date(2014, 5, 4, 9, 30, 0),
    today = new Date(2014, 5, 2, 9, 30, 0);

if (datePhp > today) {
    var difference = (datePhp.getTime() - today.getTime()) / (1000*60*60);
    document.getElementById('res').innerHTML = difference;
}

Here is a fiddle for the same. http://jsfiddle.net/tQ9jW/

If you can explain why you are doing all these complex things if you just need the difference in time we could look into debugging the code.