PHP时差不起作用

时间:2016-08-24 19:11:32

标签: javascript php mysql

更新

感谢Mike C的帮助。数据库是在EndTime而不是endTime下构建的。

-

我知道这可能会被问过几次 - 我已经阅读了几个教程并且我得到了错误。会爱一些帮助!

我在MySQL数据库中有两次。我正在尝试从time2到time1采用时间差来以hours:minutes:seconds格式显示给客户端。数据库中的两次都是DateTime格式。

如果closingTime变量未完成,我有一个实时计时器(从服务器时间开始计时1)显示在网页中。 If语句工作正常。一旦我们在页面上建立了Time1和Time2时间,页面将加载一个时间,但它是与服务器时间的StartTime差异的当前快照。

enter image description here

在上图中,左侧是实时javascript计时器,右侧只是拍摄快照而不是显示startTime和COGtime之间的时差

变量:(来自MySQL中的DateTime)

$startTime = $row['startTime'];
$COGTime = $row['endTime'];

如果仅输入startTime :(图像左侧的工作时间)

echo '<div id="StartTimeFromServer"></div>';

如果同时输入StartTime和endTime :( BROKEN:当前图像的右侧..应该采用COGTime - startTime)

                        $date_a = new DateTime($startTime);
                        $date_b = new DateTime($COGTime);

                        $interval = date_diff($date_a,$date_b);

                        echo $interval->format('%h:%i:%s');

反JS脚本

      <script type="text/javascript">
  function CountUp(initDate, id){
      this.beginDate = new Date(initDate);
      this.countainer = document.getElementById(id);
      this.numOfDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
      this.borrowed = 0, this.years = 0, this.months = 0, this.days = 0;
      this.hours = 0, this.minutes = 0, this.seconds = 0;
      this.updateNumOfDays();
      this.updateCounter();
  }

  CountUp.prototype.updateNumOfDays=function(){
      var dateNow = new Date();
      var currYear = dateNow.getFullYear();
      if ( (currYear % 4 == 0 && currYear % 100 != 0 ) || currYear % 400 == 0 ) {
          this.numOfDays[1] = 29;
      }
      var self = this;
      setTimeout(function(){self.updateNumOfDays();}, (new Date((currYear+1), 1, 2) - dateNow));
  }

  CountUp.prototype.datePartDiff=function(then, now, MAX){
      var diff = now - then - this.borrowed;
      this.borrowed = 0;
      if ( diff > -1 ) return diff;
      this.borrowed = 1;
      return (MAX + diff);
  }

  CountUp.prototype.calculate=function(){
      var currDate = new Date();
      var prevDate = this.beginDate;
      this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60);
      this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60);
      this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24);
      this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]);
      this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12);
      this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(),0);
  }

  CountUp.prototype.addLeadingZero=function(value){
      return value < 10 ? ("0" + value) : value;
  }

  CountUp.prototype.formatTime=function(){
      this.seconds = this.addLeadingZero(this.seconds);
      this.minutes = this.addLeadingZero(this.minutes);
      this.hours = this.addLeadingZero(this.hours);
  }

  CountUp.prototype.updateCounter=function(){
      this.calculate();
      this.formatTime();
      this.countainer.innerHTML ="" + this.hours + "" + (this.hours == 1? ":" : ":") + "" +
          "" + this.minutes + "" + (this.minutes == 1? ":" : ":") + "" +
          "" + this.seconds + "" + (this.seconds == 1? "" : "") + "";
      var self = this;
      setTimeout(function(){self.updateCounter();}, 1000);
  }

  window.onload=function(){ 
                            new CountUp('<?php echo $startTime // downtime start from db ?>', 'StartTimeFromServer'); }
  </script>

0 个答案:

没有答案