日历小算法:

日历小算法:

<?php

echo "<table align='center' border='1'>";

echo "<th style='background-color:green;'>日</th>";
echo "<th style='background-color:green;'>一</th>";
echo "<th style='background-color:green;'>二</th>";
echo "<th style='background-color:green;'>三</th>";
echo "<th style='background-color:green;'>四</th>";
echo "<th style='background-color:green;'>五</th>";
echo "<th style='background-color:green;'>六</th>";

$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
$day = isset($_GET['day']) ? $_GET['day'] : date('d');

$startWeek = date('w', mktime(0,0,0,$month,1,$year)); // 当月1号是星期中的第几天 param:hour minute second month day year
//w 星期中的第几天

echo "今天是$year 年-$month 月-$day 日 ";

$days = date('t', mktime(0,0,0,$month,1,$year)); // 当月有多少天

echo "<tr>";
for ($i=0; $i<$startWeek; $i++) {
echo "<td></td>"; //补几个空位
}


for ($j=1; $j<=$days; $j++) {
$i++;

if ($day == $j) {
echo "<td style='background-color:red;'>$j</td>";
} else {
echo "<td>$j</td>";
}
if ($i%7 == 0) {
echo "</tr><tr>"; //够7个换行
}

}

while($i%7 != 0) {
echo "<td></td>"; //最后补空格
$i++;
}

echo "<tr/>";
echo "</table>";

header('content-type:text/html;charset=utf-8');
原文地址:https://www.cnblogs.com/godrain/p/4414135.html