【发布时间】:2020-11-11 04:51:57
【问题描述】:
我正在尝试采用一个 html 代码来绘制一个没有事件的日历到一个在日历上显示事件的代码。我知道代码构建了 $weeks 数组,该数组稍后在用于绘制表格的代码中。如果在屏幕上按下 ,日历会前后切换月份。所以我的想法是一旦按下 就会重建 $weeks 数组,并且代码会重新绘制日历。 代码中a-tag中的href在哪里指向一次被按下。按下 ) 后代码是否会跳转到 $prev ($next) 行?代码来自以下网站https://codingwithsara.com/how-to-code-calendar-in-php/#Video。我还在下面发布了代码。
我想在该日历上显示我的事件是读取我的数据库提取事件,然后使用年、月和日期循环遍历它们以正确显示在日历的当前页面上。
谢谢。
阿金
<?php
// Set your timezone
date_default_timezone_set('Asia/Tokyo');
// Get prev & next month
if (isset($_GET['ym'])) {
$ym = $_GET['ym'];
} else {
// This month
$ym = date('Y-m');
}
// Check format
$timestamp = strtotime($ym . '-01');
if ($timestamp === false) {
$ym = date('Y-m');
$timestamp = strtotime($ym . '-01');
}
// Today
$today = date('Y-m-j', time());
// For H3 title
$html_title = date('Y / m', $timestamp);
// Create prev & next month link mktime(hour,minute,second,month,day,year)
$prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp)));
$next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));
// You can also use strtotime!
// $prev = date('Y-m', strtotime('-1 month', $timestamp));
// $next = date('Y-m', strtotime('+1 month', $timestamp));
// Number of days in the month
$day_count = date('t', $timestamp);
// 0:Sun 1:Mon 2:Tue ...
$str = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
//$str = date('w', $timestamp);
// Create Calendar!!
$weeks = array();
$week = '';
// Add empty cell
$week .= str_repeat('<td></td>', $str);
for ( $day = 1; $day <= $day_count; $day++, $str++) {
$date = $ym . '-' . $day;
if ($today == $date) {
$week .= '<td class="today">' . $day;
} else {
$week .= '<td>' . $day;
}
$week .= '</td>';
// End of the week OR End of the month
if ($str % 7 == 6 || $day == $day_count) {
if ($day == $day_count) {
// Add empty cell
$week .= str_repeat('<td></td>', 6 - ($str % 7));
}
$weeks[] = '<tr>' . $week . '</tr>';
// Prepare for new week
$week = '';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Calendar</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
<style>
.container {
font-family: 'Noto Sans', sans-serif;
margin-top: 80px;
}
h3 {
margin-bottom: 30px;
}
th {
height: 30px;
text-align: center;
}
td {
height: 100px;
}
.today {
background: orange;
}
th:nth-of-type(1), td:nth-of-type(1) {
color: red;
}
th:nth-of-type(7), td:nth-of-type(7) {
color: blue;
}
</style>
</head>
<body>
<div class="container">
<h3><a href="?ym=<?php echo $prev; ?>"><</a> <?php echo $html_title; ?> <a href="?ym=<?php echo $next; ?>">></a></h3>
<table class="table table-bordered">
<tr>
<th>S</th>
<th>M</th>
<th>T</th>
<th>W</th>
<th>T</th>
<th>F</th>
<th>S</th>
</tr>
<?php
foreach ($weeks as $week) {
echo $week;
}
?>
</table>
</div>
</body>
</html>
【问题讨论】:
-
不确定我是否理解您的问题。如果你只想知道
<h3>中的链接指向哪里,可以查看href属性:它们指向当前页面,ym查询参数设置为$prev或$next的值.这就是您在第 6 行的代码检查以确定要显示的月份的内容。 -
so prev(即
-
查询参数是通过 URL 传递给网页的选项。例如:
http://example.com/test.php?something=this&somethingElse=that。?将 URL 与参数分开,&将一个参数与另一个分开。在 PHP 中,可以使用$_GET读取这些查询参数。在此示例中,$_GET['something']将是“this”,$_GET['somethingElse']将是“that”。在您的脚本中,$prev是上个月(例如“2020-10”),因此<链接正在使用?ym=2020-10加载当前 URL,并且您的脚本正在第 6 行检查该链接。 -
感谢 rickdenhaan,正在处理您的评论