1, Use the mktime() function to get the number of days in the current month and the week on the 1st of the month (2018/3/13)
<?php $day=date("t",mktime(0,0,0,$mon,1,$year));//当前月的天数 31 $w=date("w",mktime(0,0,0,$mon,1,$year));//当月1号的星期几 4
2, Output the header information of the calendar
<?php echo"<div align='center'>"; echo"<table border='0'>"; echo"<h3><div>{$year}年{$mon}月</div></h3>"; echo "<tr>"; echo "<th>日</th>"; echo "<th class='td1'>一</th>"; echo "<th class='td1'>二</th>"; echo "<th class='td1'>三</th>"; echo "<th class='td1'>四</th>"; echo "<th class='td1'>五</th>"; echo "<th>六</th>"; echo "</tr>";
3, Traverse the output calendar
Analysis:
From the mktime() function, we can know the total number of days in the current month. The day of the week is the 1st of the month. It only needs to be less than the total number of days in the current month. Each time, 7-day traversal output is performed.
Start from the day of the week corresponding to the 1st of the month and add 1 every day. The 7-day cycle is one row of data, so the calendar comes out
Code:
<?php $d=1; while($d<=$day){ echo"<tr onmouseOver='overTr(this)'onmouseOut='outTr(this)'>"; for($i=1;$i<=7;$i++){//循环输出7天信息 if($d<=$day&&($w<$i||$d!=1)){ echo "<th><a href=''>{$d}</a></th>"; $d++; }else{ echo"<th> </th>"; } } }
The running results are as follows:
Next Section