先来看下效果:
下面是代码:
<?php header("Content-Type:text/html;charset=utf-8"); date_default_timezone_set("Asia/Shanghai"); $gety = isset($_GET["y"])?(int)$_GET["y"]:date('y');//获取年份 $getm = isset($_GET["m"])?(int)$_GET["m"]:date('m');//获取月份 $dq_date = new DateTime("$gety-$getm-01");//获取当前月份第一天的时间格式 $in_date = new DateTime();//今天的时间格式 $year = $dq_date->format("Y");//当前的年份 $month = $dq_date->format("m");//当前的月份 $day = $in_date->format("d");//获取今天是几号 $days = $dq_date->format("t");//当前月份的总天数 $week = $dq_date->format("w");//每个月的一号是星期几 $before_y = $year-1; //获取上一年的年份 $after_y = $year+1; //获取下一年的年份 $before_m = $month-1; //获取上个月的月份 $after_m = $month+1; // 获取下个月的月份 $before_m_y = $after_m_y = $year; //上下月份的链接里面的年份 //如果上个月的链接里面的月份小于1,年份减一,月份改为12(上一年的最后一个月) if($before_m<1){ $before_m_y = $year-1; $before_m = 12; } //如果下个月的链接里面的月份大于12,年份加一,月份改为1(下一年的第一个月) if($after_m>12){ $after_m_y = $year+1; $after_m = 1; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>PHP日历</title> <style type="text/css"> table.hovertable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #999999;border-collapse: collapse;text-align:center;} table.hovertable th {background-color:#c3dde0;border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;} table.hovertable th a{color:#333333;text-decoration:none;} table.hovertable tr {background-color:#d4e3e5;} table.hovertable td {border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;} /*今天日期高亮*/ .ymd<?php echo $in_date->format("Y").$in_date->format("d").$day;?>{background-color:#ffff66;} /*今天是几号高亮*/ .ymd<?php echo $year.$month.$day;?>{background-color:#ffff66;} </style> </head> <body> <table class="hovertable"> <tr> <th><a href="?<?php echo "y={$before_y}&m={$month}";?>"><<上一年</a></th> <th> <a href="?<?php echo "y={$before_m_y}&m={$before_m}";?>"><<上个月</a> </th> <th><?php echo $year."-".$month."-".$day ?></th> <th class="select"> <select onchange="window.location='?m=<?php echo $month;?>&y='+this.value;"> <?php for ( $i = ($year-20); $i <= ($year+20); $i++) { if($year==$i){$selected = 'selected';}else{$selected = '';} echo "<option value='{$i}' {$selected}>{$i}</option>"; } ?> </select> </th> <th class="select"> <select onchange="window.location='?y=<?php echo $year;?>&m='+this.value;"> <?php for ($i=1; $i <= 12; $i++) { if($month==$i){$selected = 'selected';}else{$selected = '';} echo "<option value='{$i}' {$selected}>{$i}</option>"; } ?> </select> </th> <th><a href="?<?php echo "y={$after_m_y}&m={$after_m}";?>">下个月>></a></th> <th><a href="?<?php echo "y={$after_y}&m={$month}";?>">下一年>></a></th> </tr> <tr> <th>星期日</th> <th>星期一</th> <th>星期二</th> <th>星期三</th> <th>星期四</th> <th>星期五</th> <th>星期六</th> </tr> <tr> <?php //补齐1号之前的td占位格 for ($i=0; $i < $week; $i++) { echo "<td></td>"; } //循环当前月份的天数 for ($i=1; $i <= $days; $i++) { //周一换行 if($i > 1 && ($i+$week)%7==1 ){ echo "</tr><tr>"; } echo "<td class='ymd{$year}{$month}{$i}' onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">{$i}</td>"; } //补齐月底之后的td占位格 $week_ys = ($days+$week)%7; if( $week_ys > 0 ){ for ($i=0; $i < (7-$week_ys); $i++) { echo '<td></td>'; } } ?> </tr> </table> </body> </html>
就这样,一个好看的PHP日历就完成了。
本文为dsg112社区原创文章,转载无需和我联系,但请注明来自dsg112社区. www.dsg112.com