欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP输出日历表代码实例

程序员文章站 2022-05-12 13:00:14
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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");
 
 function printmon($year, $mon)
 {
 date_default_timezone_set("asia/shanghai"); 
 global $month;
 global $enmonth;
 global $week;
 global $backcolor;
 
 $startdate =strtotime("1 $enmonth[$mon] $year"); //获取查询的年月
 $enddate = strtotime("+1 month",$startdate);   //获取下一个月的开始日期作为月历输出的截止时间
 $thedate = getdate($startdate); //把日期转化为字符串格式
 $color = $backcolor[$mon]; //设置月历的背景颜色
 
 echo("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"$color\">");
 $ym = $year . "年". $month[$mon];
 echo("<caption><h1>$ym</h1></caption>");
 echo("<tr>");
 for ($i=0; $i<7; $i++) //输出星期几
 {
 echo("<td width=\"90\", height=\"40\" align=\"center\" >");
 echo("<h2>$week[$i]</h2>");
 echo("</td>");
 }
 echo("</tr>");


 $theweek = $thedate[wday];//判断当天是星期几
 for ($i=0; $i<6; $i++)
 {
 echo("<tr>");
 for ($j=0; $j<7; $j++)
 {
 echo("<td width=\"90\", height=\"40\" align=\"center\" >");
 if ($startdate < $enddate && $theweek == $j)//把日期输出到对应的星期几所在列,并注意不要超出本月日期
 {
 $theday = $thedate[mday];
 echo("<h2>$theday</h2>");
 $startdate = strtotime("+1 day", $startdate); //日期前移1天
 $thedate = getdate($startdate);//更新日期
 $theweek = ($theweek + 1) % 7;//更新星期
 }
 echo("</td>");
 }
 echo("</tr>");
 if ($startdate == $enddate) //如果已经输出全部日期,结束循环
 {
 $i = 6;
 }
 }
 
 echo("</table");
 } 
?>


</head>


<body>


<form method="post" action="<?php echo $_server['php_self'];?>">
<h1>请输入要查看的年号和月份(查询范围为1970年1月1日至2038年)</h1>
<input type="text" name="myyear">年<input type="text" name="mymonth">月
<input type="submit">
</form>


<?php
 $year = $_post['myyear']; 
 $month = $_post['mymonth'];
 if (is_numeric($year) && $year >= 1970 && $year <2038)
 {
 if (is_numeric($month) && $month >= 1 && $month <=12)
 {
 printmon($year, $month);
 }
 else if($month != null)
 {
 echo("月份不对" . "<br />");
 }
 }
 else if($year != null)
 {
 echo("年份不对" . "<br />");
 }
?>


</body>
</html>