DatePicker,FullCalendar和MySQL事件日期最终只有一天

时间:2014-08-06 12:03:06

标签: javascript php jquery mysql datepicker

我正在使用DatePicker,FullCalendar和MySQL的组合。

DatePicker初始化

<script type='text/javascript'>
  $(document).ready(function() {
     $('#dp1').datepicker({
         autoclose: true,
         format: "yyyy-mm-dd"
     });
  });
</script>

FullCalendar初始化

<script type='text/javascript'>
  $(document).ready(function() {
    $('#calendar').fullCalendar({
      eventSources: [
        {
          url: 'data.php',
          type: 'POST'
        }
      ]
    })
  });
</script>

FullCalendar事件源'data.php'

<?php
$dbh = new PDO('mysql:host=localhost;dbname=calendar', 'user', 'pass');
$stmt = $dbh->prepare("SELECT * FROM bookings");

$stmt->execute();

$return_array = array();
$event_array;

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $event_array = array();

  $event_array['id'] = $row['id'];
  $event_array['title'] = $row['forename'] . " " . $row['surname'];
  $event_array['start'] = $row['start'];
  $event_array['end'] = $row['end'];
  $event_array['allDay'] = true;

  array_push($return_array, $event_array);
}

echo json_encode($return_array);
?>

我的问题是我可以在MySQL数据库中添加一个事件,所选日期 2014-08-04 2014-08-08 但是一旦添加了这个日历上显示为 2014-08-04 2014-08-07 ,为期1天。

这是MySQL / PHP代码:

<?php
session_start();
$dbh = new PDO('mysql:host=localhost;dbname=calendar', 'user', 'pass');

$stmt = $dbh->prepare("INSERT INTO bookings (forename, surname, start, end) VALUES (:forename, :surname, STR_TO_DATE(:start,'%Y-%m-%d'), STR_TO_DATE(:end,'%Y-%m-%d'))");

$stmt->bindParam(':forename', $forename);
$stmt->bindParam(':surname', $surname);
$stmt->bindParam(':start', $start);
$stmt->bindParam(':end', $end);

$forename = $_POST['inputForename'];
$surname = $_POST['inputSurname'];
$start = $_POST['dp1'];
$end = $_POST['dp2'];

$stmt->execute();

header("Location: /");
?>

我想我在日期转换到MySQL时做错了什么但是看不清楚。

1 个答案:

答案 0 :(得分:1)

老问题,但我想如果有人偶然发现它,我会写这个......

您需要做的是在初始化中设置nextDayThreshold

nextDayThreshold: "00:00:00"

然后确保您输出的日期有时间,我通过将T23:59添加到我的日期来完成此操作,即2014-08-04将成为2014-08-04T23:59

有关详细信息,请参阅以下答案:

fullCalendar multi-day event spans 1 day too short

fullCalendar multi-day event spans 1 day too short