0

我有一个 php 日期,我想要关于 php 日期和今天日期的区别。代码为: function countdown(counter, year, month, day, hour, minute, second){
var datePhp = new Date(year, month, day, hour, minute, second); var 今天=新日期();变天= 0; var小时=0;var分钟=0;变量秒=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)
        }

代码运行,但运行错误,因为总是说不到 24 小时。例如,如果 phpDate 是 4/06/2014 09:30(其中 4 是欧洲日,6 是六月),而今天是 2/06/2014 09:30(其中 2 是欧洲日,6 是六月)。代码说phpDate只有24小时。

html中的代码是:

  <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> 
4

1 回答 1

-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

对我来说看起来不错,从 2 月 6 日到 4 月 6 日的 1416 小时

我不完全理解你的代码,但是当我用下面的代码做这件事时,我得到了48几个小时看起来不错

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;
}

这是相同的小提琴。http://jsfiddle.net/tQ9jW/

如果您能解释为什么要做所有这些复杂的事情,如果您只需要时间差异,我们可以研究调试代码。

于 2014-06-02T08:28:00.633 回答