PHP学习笔记之万年历
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>万年历</title><?php $MONTH = array("元月","壹 月","贰 月","叁 月","肆 月","伍 月","陆 月","柒 月","捌 月","玖 月","拾 月","拾壹月","拾贰月"); $enMONTH = array("元月","January" ,"February" ,"Marcy" ,"April" ,"May" ,"June" ,"July" ,"August" ,"September" ,"October" ,"November" ,"December"); $WEEK = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); $BACKCOLOR = array("#FFC" , "#FFF" , "#9F6" , "#FFC" , "#6F0" , "#6F6" , "#F90" , "#F06" , "#F00" , "#FC3" , "#FF6" , "#F99"); $ChineseNum = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九"); function ChangeNum($str) { global $ChineseNum; for ($i=0; $i<=9; $i++) { $s = (string)$i; $str = str_replace($s, $ChineseNum[$i], $str); } return $str; } function MonthDays($year, $month)//根据输入的年号和月份,返回该月的天数 { switch($month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (($year%4 == 0 && $year%100 != 0) || $year%400 == 0) return 29; else return 28; default: die("这是一个错误的月份!"); } } function WeekDay($year, $month, $day) //根据输入的日期,返回对应的星期 { $commonYear = 0; $leapYear = 0; for ($i=1; $i<$year; $i++) { if (($i%4 == 0 && $i%100 != 0) || $i%400 == 0) { $leapYear++; } else { $commonYear++; } } $sum = 366 * $leapYear + 365 * $commonYear; for ($i=1; $i<$month; $i++) { $sum += MonthDays($year, $i); } $sum += $day; //获得总天数 return (int) $sum % 7; } function PrintMon($year, $month) { global $MONTH; global $enMONTH; global $WEEK; global $BACKCOLOR; $color = $BACKCOLOR[$month]; //设置月历的背景颜色 echo("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"$color\">"); $ym = ChangeNum($year) . "年 ". $MONTH[$month]; echo("<caption><h1>$ym</h1></caption>"); echo("<tr>"); for ($i=0; $i"); echo("<h2>$WEEK[$i]</h2>"); echo(""); } echo("</tr>"); $theDay = 1; $numOfDays = MonthDays($year, $month);//根据输入的年号和月份,返回该月的天数 $theWeek = WeekDay($year, $month, $theDay); //根据输入的日期,返回对应的星期几 for ($i=0; $i"); for ($j=0; $j"); if ($theDay $theDay"); $theDay++; $theWeek = ($theWeek + 1) % 7;//更新星期 } echo(""); } echo(""); if ($theDay > $numOfDays) //如果已经输出全部日期,结束循环 { $i = 6; } } echo(""); } ?>