这篇文章主要讲解了“PHP怎么实现输出日历表”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PHP怎么实现输出日历表”吧!
<!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><h2>$ym</h2></caption>"); echo("<tr>"); for ($i=0; $i<7; $i++) //输出星期几 { echo("<td width=\"90\", height=\"40\" align=\"center\" >"); echo("<h3>$WEEK[$i]</h3>"); 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("<h3>$theDay</h3>"); $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'];?>"> <h2>请输入要查看的年号和月份(查询范围为1970年1月1日至2038年)</h2> <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>
感谢各位的阅读,以上就是“PHP怎么实现输出日历表”的内容了,经过本文的学习后,相信大家对PHP怎么实现输出日历表这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。