为什么这个日期会导致错误?

So not so long ago I made my own booking system which displays a calendar on a page with the dates that have been booked but I noticed recently, if one of the bookings goes across two months e.g. 29th March to 4th April the whole thing just crashes / doesn't load and I'm really confused as to why. The date still gets entered into my database but then it breaks.

Here is the code for displaying the calendar (Note: I got most of this code from a CodePen and edited it accordingly):

<?php

session_start();
ob_start();

$host = "myhostname"; // Host name 
$username = "myusername"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "mydatabase"; // Database name 
$tbl_name = "booking"; // Table name 

// Connect to server and select databse.
$conn = mysqli_connect($host, $username, $password)or die("cannot connect"); 
mysqli_select_db($conn, $db_name)or die("cannot select DB");

$sql = "SELECT * FROM $tbl_name";

$result = mysqli_query($conn, $sql);

?>

<script>
    !function() {

  var today = moment();

  function Calendar(selector, events) {
    this.el = document.querySelector(selector);
    this.events = events;
    this.current = moment().date(1);
    this.events.forEach(function(ev) {
     ev.date = moment(ev.date);
    });
    this.draw();
    var current = document.querySelector('.today');
    if(current) {
      var self = this;
      window.setTimeout(function() {
        self.openDay(current);
      }, 500);
    }

  }

  Calendar.prototype.draw = function() {
    //Create Header
    this.drawHeader();

    //Draw Month
    this.drawMonth();
  }

  Calendar.prototype.drawHeader = function() {
    var self = this;
    if(!this.header) {
      //Create the header elements
      this.header = createElement('div', 'header');
      this.header.className = 'header';

      this.title = createElement('h3');

      var right = createElement('div', 'right');
      right.addEventListener('click', function() { self.nextMonth(); });

      var left = createElement('div', 'left');
      left.addEventListener('click', function() { self.prevMonth(); });

      document.addEventListener("keydown", function(event) {
            if (event.keyCode === 39) {
                event.preventDefault();
                self.nextMonth();
            } else if (event.keyCode === 37) {
                event.preventDefault();
                self.prevMonth();
            }
      });

      //Append the Elements
      this.header.appendChild(this.title); 
      this.header.appendChild(right);
      this.header.appendChild(left);
      this.el.appendChild(this.header);
    }

    this.title.innerHTML = this.current.format('MMMM YYYY');
  }

  Calendar.prototype.drawMonth = function() {
    var self = this;


    if(this.month) {
      this.oldMonth = this.month;
      this.oldMonth.className = 'month out ' + (self.next ? 'next' : 'prev');
      this.oldMonth.addEventListener('webkitAnimationEnd', function() {
        self.oldMonth.parentNode.removeChild(self.oldMonth);
        self.month = createElement('div', 'month');
        self.backFill();
        self.currentMonth();
        self.fowardFill();
        self.el.appendChild(self.month);
        window.setTimeout(function() {
          self.month.className = 'month in ' + (self.next ? 'next' : 'prev');
        }, 16);
      });
    } else {
        this.month = createElement('div', 'month');
        this.el.appendChild(this.month);
        this.backFill();
        this.currentMonth();
        this.fowardFill();
        this.month.className = 'month new';
    }
  }

  Calendar.prototype.backFill = function() {
    var clone = this.current.clone();
    var dayOfWeek = clone.day();

    if(!dayOfWeek) { return; }

    clone.subtract('days', dayOfWeek);

    for(var i = dayOfWeek; i > 1 ; i--) {
      this.drawDay(clone.add('days', 1));
    }
  }

  Calendar.prototype.fowardFill = function() {
    var clone = this.current.clone().add('months', 1).subtract('days', 1);
    var dayOfWeek = clone.day();

    if(dayOfWeek === 7) { return; }

    for(var i = dayOfWeek; i < 7 ; i++) {
      this.drawDay(clone.add('days', 1));
    }
  }

  Calendar.prototype.currentMonth = function() {
    var clone = this.current.clone();

    while(clone.month() === this.current.month()) {
      this.drawDay(clone);
      clone.add('days', 1);
    }
  }

  Calendar.prototype.getWeek = function(day) {
    if(!this.week || day.day() === 1) {
      this.week = createElement('div', 'week');
      this.month.appendChild(this.week);
    }
  }

  Calendar.prototype.drawDay = function(day) {
    var self = this;
    this.getWeek(day);

    //Outer Day
    var outer = createElement('div', this.getDayClass(day));
    outer.addEventListener('click', function() {
      self.openDay(this);
    });

    //Day Name
    var name = createElement('div', 'day-name', day.format('ddd'));

    //Day Number
    var number = createElement('div', 'day-number', day.format('DD'));


    //Events
    var events = createElement('div', 'day-events');
    this.drawEvents(day, events);


    outer.appendChild(name);
    outer.appendChild(number);
    outer.appendChild(events);
    this.week.appendChild(outer);
  }

  Calendar.prototype.drawEvents = function(day, element) {
    if(day.month() === this.current.month()) {
      var todaysEvents = this.events.reduce(function(memo, ev) {
        if(ev.date.isSame(day, 'day')) {
          memo.push(ev);
        }
        return memo;
      }, []);

      todaysEvents.forEach(function(ev) {
        var evSpan = createElement('span', ev.color);
        element.appendChild(evSpan);
      });
    }
  }

  Calendar.prototype.getDayClass = function(day) {
    classes = ['day'];
    if(day.month() !== this.current.month()) {
      classes.push('other');
    } else if (today.isSame(day, 'day')) {
      classes.push('today');
    }
    return classes.join(' ');
  }

  Calendar.prototype.openDay = function(el) {
    var details, arrow;
    var dayNumber = +el.querySelectorAll('.day-number')[0].innerText || +el.querySelectorAll('.day-number')[0].textContent;
    var day = this.current.clone().date(dayNumber);

    var currentOpened = document.querySelector('.details');

    //Check to see if there is an open detais box on the current row
    if(currentOpened && currentOpened.parentNode === el.parentNode) {
      details = currentOpened;
      arrow = document.querySelector('.arrow');
    } else {
      //Close the open events on differnt week row
      //currentOpened && currentOpened.parentNode.removeChild(currentOpened);
      if(currentOpened) {
        currentOpened.addEventListener('webkitAnimationEnd', function() {
          currentOpened.parentNode.removeChild(currentOpened);
        });
        currentOpened.addEventListener('oanimationend', function() {
          currentOpened.parentNode.removeChild(currentOpened);
        });
        currentOpened.addEventListener('msAnimationEnd', function() {
          currentOpened.parentNode.removeChild(currentOpened);
        });
        currentOpened.addEventListener('animationend', function() {
          currentOpened.parentNode.removeChild(currentOpened);
        });
        currentOpened.className = 'details out';
      }

      //Create the Details Container
      details = createElement('div', 'details in');

      //Create the arrow
      var arrow = createElement('div', 'arrow');

      //Create the event wrapper

      details.appendChild(arrow);
      el.parentNode.appendChild(details);
    }

    var todaysEvents = this.events.reduce(function(memo, ev) {
      if(ev.date.isSame(day, 'day')) {
        memo.push(ev);
      }
      return memo;
    }, []);

    this.renderEvents(todaysEvents, details);

    arrow.style.left = el.offsetLeft - el.parentNode.offsetLeft + 27 + 'px';
  }

  Calendar.prototype.renderEvents = function(events, ele) {
    //Remove any events in the current details element
    var currentWrapper = ele.querySelector('.events');
    var wrapper = createElement('div', 'events in' + (currentWrapper ? ' new' : ''));

    events.forEach(function(ev) {
      var div = createElement('div', 'event');
      var span = createElement('span', '', ev.eventName);

      div.appendChild(span);
      wrapper.appendChild(div);
    });

    if(!events.length) {
      var div = createElement('div', 'event empty');
      var span = createElement('span', '', 'No Booking');

      div.appendChild(span);
      wrapper.appendChild(div);
    }

    if(currentWrapper) {
      currentWrapper.className = 'events out';
      currentWrapper.addEventListener('webkitAnimationEnd', function() {
        currentWrapper.parentNode.removeChild(currentWrapper);
        ele.appendChild(wrapper);
      });
      currentWrapper.addEventListener('oanimationend', function() {
        currentWrapper.parentNode.removeChild(currentWrapper);
        ele.appendChild(wrapper);
      });
      currentWrapper.addEventListener('msAnimationEnd', function() {
        currentWrapper.parentNode.removeChild(currentWrapper);
        ele.appendChild(wrapper);
      });
      currentWrapper.addEventListener('animationend', function() {
        currentWrapper.parentNode.removeChild(currentWrapper);
        ele.appendChild(wrapper);
      });
    } else {
      ele.appendChild(wrapper);
    }
  }

  Calendar.prototype.drawLegend = function() {
    var calendars = this.events.map(function(e) {
      return e.calendar + '|' + e.color;
    }).reduce(function(memo, e) {
      if(memo.indexOf(e) === -1) {
        memo.push(e);
      }
      return memo;
    }, []).forEach(function(e) {
      var parts = e.split('|');
      var entry = createElement('span', 'entry ' +  parts[1], parts[0]);
    });
  }

  Calendar.prototype.nextMonth = function() {
    this.current.add('months', 1);
    this.next = true;
    this.draw();
  }

  Calendar.prototype.prevMonth = function() {
    this.current.subtract('months', 1);
    this.next = false;
    this.draw();
  }

  window.Calendar = Calendar;

  function createElement(tagName, className, innerText) {
    var ele = document.createElement(tagName);
    if(className) {
      ele.className = className;
    }
    if(innerText) {
      ele.innderText = ele.textContent = innerText;
    }
    return ele;
  }
}();

!function() {
  var data = [
      <?php
      $colour = 'blue';
      while($rows = mysqli_fetch_array($result)) {
          if($rows['status'] == 'Confirmed') {
              $colour = 'red';
          } else {
              $colour = 'blue';
          }
          ?>
          { eventName: 'Booking Start', calendar: 'Booking <?php echo $rows['status']; ?>', color: '<?php echo $colour; ?>', date: '<?php echo $rows['arrive_date']?>'},
          <?php
          $date = date('Y-m-d', strtotime($rows['arrive_date']));
          while ($date < date('Y-m-d', strtotime($rows['depart_date']. '-1 day'))) {
              $date++;
              ?>
              { eventName: 'Booked', calendar: 'Booking <?php echo $rows['status']; ?>', color: '<?php echo $colour; ?>', date: '<?php echo $date;?>'},
              <?php
          }
          ?>


    { eventName: 'Booking End', calendar: 'Booking <?php echo $rows['status']; ?>', color: '<?php echo $colour; ?>', date: '<?php echo $rows['depart_date']; ?>'},

    <?php
      }
      ?>
  ];

  var calendar = new Calendar('#calendar', data);

}();
</script>

EDIT: In case you need further help this is what an example looks like from the table in the database: Table example

I can't work out what is causing this problem, I believe it is something to do with the events but I don't know. I was hoping someone a little more experienced could help me out a little.

To see what it looks like visually you can see it live here: https://ryan-simms.com/booking/bookings

EDIT: I just found out, doing a test, that if I put in 28th February to the 1st March it doesn't crash but if I change it so it ends on the 2nd March it crashes, really odd.

dongshen3352
dongshen3352 我将您的解决方案移至社区维基解答。
2 年多之前 回复
dqu92800
dqu92800 我想我找到了错误的部分,它是开始和结束日期之间的日期导致问题(while循环的地方)我正在试图弄清楚如何改变它。
2 年多之前 回复
doucanrui1735
doucanrui1735 问题是代码太多,我们甚至无法开始调试。尝试缩小问题范围,只关注相关部分。
2 年多之前 回复
duan7772
duan7772 这是CodePencodepen.io/peanav/pen/ulkof,我不太确定日历应该用于多个日期而只是单个日期。
2 年多之前 回复
dream1849
dream1849 CodePen代码是否有同样的问题?链接是什么?如果不是:你改变了什么?
2 年多之前 回复

1个回答



OP的解决方案。</ em> </ p>

通过编辑<代码解决 > while </ code>循环并使用不同的方法获取两个日期之间的所有日期。 示例代码:</ p>

  $ start = date('Ym-d',strtotime($ rows ['arrival_date']。'+ 1天')); 
$ end = date('Ym-d',strtotime($ rows ['depart_date']。' - 1天'));
while($ start&lt; = $ end){
?&gt;
{eventName:' 预订',日历:'预订&lt;?php echo $ rows ['status']; ?&gt;',color:'&lt;?php echo $ color; ?&gt;',date:'&lt;?php echo $ start;?&gt;'},
&lt;?php
$ start = date(“Ymd”,strtotime(“+ 1天”,strtotime($ 开始)));
}
</ code> </ pre>
</ div>

展开原文

原文

Solution by OP.

Solved by editing the while loop and using a different method for getting all dates in between two dates. Example code:

$start = date('Y-m-d', strtotime($rows['arrive_date']. '+1 day'));
$end =  date('Y-m-d', strtotime($rows['depart_date']. '-1 day'));
while ($start <= $end) {
    ?>
    { eventName: 'Booked', calendar: 'Booking <?php echo $rows['status']; ?>', color: '<?php echo $colour; ?>', date: '<?php echo $start;?>'},
    <?php
    $start = date ("Y-m-d", strtotime("+1 days", strtotime($start)));
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐