2015-12-30 87 views
1

我正在开发自己的日历事件。一切都很好,但现在我遇到了一些问题。将活动添加到日历中php

这是我的代码:

<?php 

    $timestamp = mktime(0, 0, 0, $cMonth, 1, $cYear); 

    $maxday = date("t", $timestamp); 
    $thismonth = getdate($timestamp); 
    $thisyear = getdate($timestamp); 
    $startday = $thismonth['wday']; 
    for ($i = 0; $i < ($maxday + $startday); $i++) { 
     $date = ($i - $startday + 1) . $cMonth . $cYear; 

     if (($i % 7) == 0) 
      ; 

     if ($i < $startday) 
      ; 
     elseif (($i - $startday + 1) == $cDay && $cMonth && $cYear) { 
      echo "<div class='currentDate'>" . ($i - $startday + 1) . "<br /></div>"; 
     } else { 
      echo "<a href='?day=" . ($i - $startday + 1) . "&month=" . $cMonth . "&year=" . $cYear . "'><div class='date'>" . ($i - $startday + 1) . "</div></a>"; 
     } 

     $result = $MySQLi_CON->query("SELECT * FROM events"); 
     while ($row = $result->fetch_array()) { 
      $event_date2 = $row['event_date']; 
      $date2 = date("Y-m-d", strtotime($event_date2)); 

      if (($i - $startday + 1) == $date2) { // Check if the date is the same 
       echo "<div class='date'><a href='#'>" . $date2 . "</a></div>"; // Show the event date. 
      } 
     } 
    } 
    ?> 

我试图与来自在MySQL表“事件”日期日历每天匹配。日历中与事件中的日期匹配的每个日期都应显示此事件。

编辑:其余代码:

 <?php 
     $monthNames = Array("January", "February", "March", "April", "May", "June", "July", 
     "August", "September", "October", "November", "December"); 



     if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n"); 
     if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y"); 



     $cMonth = $_REQUEST["month"]; 
     $cYear = $_REQUEST["year"]; 



     if(!isset($_REQUEST['day'])) $_REQUEST['day'] = date('d'); 
     if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n"); 
     if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y"); 

    // Rest of code 
     $cDay = $_REQUEST['day']; 
     $cMonth = $_REQUEST["month"]; 
     $cYear = $_REQUEST["year"]; 




     // Rest of code 

     ?> 
+0

那么你的问题究竟是什么? – jiboulex

+0

问题是我的代码显示“event_date”的每个日期。 – user3219200

+0

数据库中的日期字段是以时间戳,日期时间还是日期存储的? – jiboulex

回答

0

你的问题是你获取所有的事件,而不按日期过滤他们。您应该在查询中添加一个条件以仅提取发生在特定日期的事件:

$result = $MySQLi_CON->query("SELECT * FROM events WHERE event_date = '2015-12-30'"); 

例如。

在我看来,你应该首先用一个请求获取当月的所有事件,然后显示你的日历。你也可以使用datetime对象,这使得它easyer处理在PHP日期

http://php.net/manual/en/class.datetime.php

首先,你可以设置你的日期:

$monthInterval = new \DateInterval('P1M'); 
$dayInterval = new \DateInterval('P1D'); 
$dateStart = new \DateTime(); 
$dateStart->setDate($cYear, $cMonth, 1); 
$dateEnd = clone $dateStart; 
// We set the date end to be the first day of the next month 
$dateEnd->add($monthInterval); 

然后,你可以取这个月的所有事件

$events = []; 
$result = $MySQLi_CON->query("SELECT * FROM events WHERE event_date >= '" . $dateStart->format('Y-m-d') . "' AND event_date <= '" . $dateEnd->format('Y-m-d') . "'"); 

// We create a multidimentional array with all the events of the month 
while ($row = $result->fetch_array()) { 
    $events[$row['event_date']][] = $row; 
} 

然后你就可以显示您的日历:

$currentDate = clone $dateStart; 
while ($currentDate < $dateEnd) { 
    // the date is available with the $currentDate variable 
    ... do what you want for each day 

    // If you want to get the events of the day, just check if they exist : 
    $currentDateFormat = $currentDate->format('Y-m-d'); 
    if (isset($events[$currentDateFormat]) { 
     // Then you can loop through your daily events : 
     foreach ($events[$currentDateFormat] as $event) { 
      .... 
     } 
    } 

    // We add one day to the current date to step to the next one 
    $currentDate->add($dayInterval); 
} 

请注意,如果您将日期字段存储为时间戳或日期时间,那么您必须调整我的代码,使其与日期字段配合使用。

+0

嗨,谢谢你的帮助。我试图让它工作,但我得到一个错误,说:“致命的错误:__克隆方法调用非...对象...”。 我会发布我的代码的其余部分,也许你可以找出问题所在。 – user3219200

+0

我编辑了我的代码,我克隆了一个不存在的对象,但是你可以看到($ dateEnd = clone $ dateStart;) – jiboulex

+0

好吧,对不起,现在我明白了。 – user3219200