gpt4 book ai didi

php - 将事件添加到日历 php

转载 作者:行者123 更新时间:2023-11-30 22:28:33 25 4
gpt4 key购买 nike

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

这是我的代码:

    <?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);
}

请注意,如果您将日期字段存储为时间戳或日期时间,您将必须调整我的代码,因为它已实现以处理日期字段。

关于php - 将事件添加到日历 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34527978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com