繁体   English   中英

将事件添加到日历php

[英]Add events to calendar 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

        ?>

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

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

例如。

我认为,您应该首先通过一个请求获取该月的所有事件,然后显示日历。 您还可以使用DateTime对象,从而更轻松地处理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);
}

请注意,如果将日期字段存储为时间戳或日期时间,则必须修改我的代码,因为它已实现为可以与日期字段一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM