0

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> 
mjosee7
  • 33
  • 5
  • Where are year, month, day, hour, minute and second defined? – bcmcfc Jun 02 '14 at 08:20
  • 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 Jun 02 '14 at 08:22
  • @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 Jun 02 '14 at 08:44

1 Answers1

-1
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.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • Thanks. It is not April and February. It's the difference about two days – mjosee7 Jun 02 '14 at 08:37
  • Thats why you should not blindly rely on the string parsing capabilities of JS. The OP clearly states which dates `4/06/2014` and `2/06/2014` represents. They are in the format `DD/MM/YYYY` and not `MM/DD/YYYY` – t.niese Jun 02 '14 at 08:38
  • ok, can you explain the logic you are trying to do, its not clear for me. Looks like you are not just finding difference in hours what is `sumeDays` and why are you doing things like `difference=difference-(86400*days)` – sabithpocker Jun 02 '14 at 08:41
  • @t.niese Yes sure. I was just doing a quick check in console. – sabithpocker Jun 02 '14 at 08:51
  • I have this http://www.mundonets.com/contador-javascript.html but, in a table about auction where in each row has a different time. The code runs right, but the days 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 programm say that we have 24 hours to date, but really is 48 hours – mjosee7 Jun 02 '14 at 09:40
  • https://developer.chrome.com/devtools/docs/javascript-debugging try to debug it line by line adding breakpoint at first line and going line by line. As i dont understand your logic its easier for yourself to debug. or come to chat http://chat.stackoverflow.com/rooms/17/javascript – sabithpocker Jun 02 '14 at 09:50