[英]How to make a basic calendar with PHP?
我是学习 PHP 的初学者,我正在尝试使用 PHP 制作基本日历。 Simpele kalender是它的外观。 它必须与 if 和/或循环一起使用。 这是我到目前为止所拥有的:
<?php
$month_start = 'Tue';
$number_days = 28;
// number of rows
$number_rows = $number_days / 7;
if ($number_days % 7 != 0) {
// number of days
$number_rows = ($number_days / 7) + 1;
}
?>
<table>
<tr>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
</tr>
<?php
for($y = 1; $y <= $number_rows;$y++){
echo "<tr>";
for($i = 1; $i <= $number_days; $i++){
if($i % 7 != 0){
echo "<td>". $i . "</td>";
}
else{
echo "<td>". $i . "</td>";
break;
}
}
echo "</tr>";
}
?>
</table>
<style>
table, th, td {
border: 1px solid black;
text-align: center;
}
</style>
我如何从星期四(月初)开始,如何在列中获取第 1 天到第 28 天? 我制作了另一个日历,但那不正确。 根据我得到的反馈,我不需要使用 gmdate、cal_days_in_month 和 mktime。 谁能帮助我 go 进入正确的方向?
一种选择是给出一周中的天数:Mon=0、Tues=1 等。您可以设置变量$start_day = 3;
其中 3 代表星期四。 然后添加它以获得总行数: $number_rows = ceil(($start_day + $number_days) / 7);
最后,创建一个变量来标记要打印的当前日期。 将其初始化为-$start_day
并仅在非负数时打印:
$number_days = 28;
$start_day = 3; // Mon=0, Tues=1, etc
// Add in $start_day her to account for the "negative" days before the 1st day
$number_rows = ceil(($start_day + $number_days) / 7);
$current_day = -$start_day;
for ($row = 0; $row < $number_rows; $row++) {
echo("<tr>");
for ($col = 0; $col < 7; $col++) {
echo("<td>");
// Only print the day number if it is not a "negative" day and is not past the end
if ($current_day++ >= 0 && $current_day <= $number_days) {
echo($current_day);
}
echo("</td>");
}
echo("</tr>");
}
您可以使用if($loopCount++ > number)在 LOOP 中开始 DAY
这是一个例子
$count = 1;
foreach($colum as $res)
{
echo'<td>';
if($count++ > 3){echo $days++;} //will start in the third loop
echo'</td>';
}
像这样申请
<?php
//TOTAL DAY ON THIS MONTH
$total_days = 30;
//WHERE DAY START
$start_days = 3;
//TOTAL COLUM OF CALENDAR
$col = 27; //27 because this will be an array, because the array has 0 so the total will be 28
for($x = 0; $x <= $col; $x++)
{
$colum[] = $x;
}
echo "<table><tr>";
$days = 1;
$count = 1;
foreach($colum as $res)
{
echo'<td>';if($count++ > $start_days){echo $days++;} echo'</td>';
if ($count++ % 7 == 0)
{
echo "</tr><tr>";
}
}
//NEX MONTH START WITH THIS
$rest_of_the_day = $total_days - $days + 1;
echo"</tr></table>";
这就是我创建日历的方式。 这会创建“非天”,即灰色(您必须自己设置样式)正方形,以便该月的第一天将在其适当的工作日开始。
$year = 2022;
$date = new DateTime("".$year."-01-01");
$weekday = (int)$date->format('N');
$weekn = (int)$date->format('W');
$day = 1;
$month = 1;
$dayLabels = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
// This creates a "table head" for each month
$thead = '<tr><th>M</th><th>W</th>';
foreach($dayLabels as $lbl) $thead .= "<th>".$lbl."</th>";
$thead .= '</tr>';
// This variable will hold the html table of the calendar
$calendar = '<table id="calendar" border="1" style="width: 550px; max-width: 100%;">';
// Loop each month until 12
for(;$month <= 12; $month++)
{
$calendar .= $thead;
$monthday = 1;
$monthmax = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$position = 0;
while($monthday <= $monthmax)
{
// Create month and week columns
if($position == 0)
{
$weekn = (int)$date->format('W');
$calendar .= "<tr><th>".$month."</th><th>".$weekn."</th>";
$position++;
} // Create "non-days"
else if($position < $weekday)
{
$calendar .= "<td class=\"nonday\"></td>";
$position++;
} // Create days
else if($position == $weekday)
{
$calendar .= "<td class=\"day\">".$monthday."</td>";
$position++;
$weekday++;
$monthday++;
$date->modify('+1 day');
if($position == 8)
{
$position = 0;
$weekday = 1;
}
}
}
// Finish "non-days"
while($position != 0)
{
$calendar .= "<td class=\"nonday\"></td>";
$position++;
if($position == 8) $position = 0;
}
$calendar .= "</tr>";
if($month != 12) $calendar .= '<td colspan="9" class="monthlinebreak"></td>';
}
return $calendar;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.