首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将事件添加到日历php中

将事件添加到日历php中
EN

Stack Overflow用户
提问于 2015-12-30 10:48:17
回答 1查看 1.1K关注 0票数 1

我正在制定自己的日历与事件。一切都很好,但现在我遇到了一些问题。

这是我的密码:

代码语言:javascript
复制
    <?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中表"events“中的日期相匹配。日历中每个在事件中具有匹配日期的日期都应显示此事件。

编辑:代码的其余部分:

代码语言:javascript
复制
     <?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

        ?>
EN

回答 1

Stack Overflow用户

发布于 2015-12-30 11:14:57

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

代码语言:javascript
复制
$result = $MySQLi_CON->query("SELECT * FROM events WHERE event_date = '2015-12-30'");

例如。

在我看来,首先应该用一个请求获取这个月的所有事件,然后显示您的日历。您还可以使用DateTime对象来处理php中的日期。

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

首先,你可以确定你的约会日期:

代码语言:javascript
复制
$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);

然后你就可以获取这个月的所有事件了。

代码语言:javascript
复制
$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;
}

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

代码语言:javascript
复制
$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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34527978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档