在同样使用php(php日历)的html代码中,href链接指向哪里



我正在尝试采用一个html代码,该代码可以绘制没有事件的日历,而该代码可以在日历上显示事件。据我所知,该代码构建了$weeks数组,该数组稍后将出现在用于绘制表格的代码中。如果<或者>在屏幕上按下。所以我的想法是$weeks数组被重构一次<或者>按下,代码将重新绘制日历。代码中a标记中的href指向once<或者>按下。代码是否跳转到$prev($next(行一次<(>(被按下?代码来自以下网站https://codingwithsara.com/how-to-code-calendar-in-php/#Video.我还在下面发布了代码。

我在日历上显示我的事件的想法是读取我的数据库提取事件,然后使用年、月和日期对它们进行循环,以便在日历的当前页面上正确显示。

谢谢。

Argyn

<?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; ?>">&lt;</a> <?php echo $html_title; ?> <a href="?ym=<?php echo $next; ?>">&gt;</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>

第一个:

if (isset($_GET['ym'])) {
$ym = $_GET['ym'];
} else {
// This month
$ym = date('Y-m');
}    
$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)));

您正在创建一个Y-m日期,但期望使用Ym格式。

第二:您正在为您的月份计算创建一种非常复杂的计算方式。您应该查看datetime类、dateinterval类和dateperiod类

您可以创建一个简单的概述:

<?php
if(isset($_GET['ym'])){
$start  = DateTime::createFromFormat('Y-m-d',$date.'-1');
}else{
$start  = new DateTime();
$start->setDate($start->format('Y'),$start->format('m'),1);
}
$start->setTime(00, 00);
$end = clone $start;
$end->setDate($end->format('Y'),$end->format('m'),$end->format('t'))->setTime(23, 59);
$prev = clone $start;
$prev->modify("-1 month");
$next = clone $end;
$next->modify("+1 day");
$interval = DateInterval::createFromDateString('1 day');

$period     = new DatePeriod($start,$interval,$end);
?>

然后你可以使用:

<a href="?ym=<?php echo $prev->format('Y-m');?>">&lt;</a>
//AND
<a href="?ym=<?php echo $next->format('Y-m');?>">&gt;</a>

对于环路:

<?php
foreach($period as $day){
echo $day->format('Y-m-d');
}
?>

代码示例

最新更新