当前位置 : 主页 > 网络编程 > PHP >

编写普通日历,显示当天和当月的情况

来源:互联网 收集:自由互联 发布时间:2021-07-03
?php date_default_timezone_set('PRC'); $tdate = getdate(); //当前时间 $tmday = $tdate['mday']; //今天几号 $twday = $tdate['wday']; //今天星期几 $mon = $tdate['mon']; //月份 $year = $tdate['year']; //今年 $sdate = getdate(mk
 
<?php 
 date_default_timezone_set('PRC');
 $tdate = getdate(); //当前时间
 $tmday = $tdate['mday']; //今天几号
 $twday = $tdate['wday']; //今天星期几
 $mon = $tdate['mon']; //月份
 $year = $tdate['year']; //今年
 $sdate = getdate(mktime(0,0,0,$mon,1)); //本月1号
 $swday = $sdate['wday']; //本月一号星期几
 if($mon==2)
 {
     if($year % 4==0 && $year % 100!=0 || $year %400 ==0)
         $days = 29;
     else
         $days = 28;
 }
 else if($mon == 4 || $mon == 6 || $mon == 9 || $mon == 11)
     $days = 30;
 else
     $days = 31;
 $edate = getdate(mktime(0,0,0,$mon,$days));//本月最后一天
 $ewday = $edate['wday']; //本月最后一天星期几
 $counts = 0; //计数器
 $html = "<table style='border: solid 1px black'>";
 $html .="<tr><th colspan='6'>{$year}年{$mon}月<th></tr>";
 $html .="<tr><th>星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr>";
 $html .="<tr>";
 for($i = 0; $i < ($swday % 7);$i++)
     $html .='<td></td>';
 while($counts != $days)
 {
     if(($swday+$counts)%7 ==0)
         $html .="<tr><td align='center' style=".($styles = (($counts+1) ==$tmday ? 'color:red;font-weight:bold' : '')).">".(++$counts).'</td>';
     else if(($swday+$counts)%7 ==6)
         $html .="<td align='center' style=".($styles = (($counts+1) ==$tmday ? 'color:red;font-weight:bold' : '')).">".(++$counts).'</td></tr>';
     else
         $html .="<td align='center' style=".($styles = (($counts+1) ==$tmday ? 'color:red;font-weight:bold' : '')).">".(++$counts).'</td>';
 }
 if($ewday!=6)
     $html .='</tr>';
 $html .='</table>';
 echo $html;
?>

网友评论