您的位置:首页 > 编程语言 > PHP开发

PHP学习笔记之万年历

2015-01-30 10:30 302 查看
<!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");
$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<7; $i++) //输出星期几
{
echo("<td width=\"90\", height=\"40\" align=\"center\" >");
echo("<h2>$WEEK[$i]</h2>");
echo("</td>");
}
echo("</tr>");

$theDay = 1;
$numOfDays = MonthDays($year, $month);//根据输入的年号和月份,返回该月的天数
$theWeek = WeekDay($year, $month, $theDay); //根据输入的日期,返回对应的星期几
for ($i=0; $i<6; $i++)
{
echo("<tr>");
for ($j=0; $j<7; $j++)
{
echo("<td width=\"90\", height=\"40\" align=\"center\" >");
if ($theDay <= $numOfDays && $theWeek == $j)//把日期输出到对应的星期几所在列,并注意不要超出本月日期
{
echo("<h2>$theDay</h2>");
$theDay++;
$theWeek = ($theWeek + 1) % 7;//更新星期
}
echo("</td>");
}
echo("</tr>");
if ($theDay > $numOfDays) //如果已经输出全部日期,结束循环
{
$i = 6;
}
}

echo("</table");
}
?>
</head>

<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<h1>请输入要查看的年号和月份</h1>
<input type="text" name="myYear">年<input type="text" name="myMonth">月
<input type="submit">
</form>

<?php
$year = (int) $_POST['myYear'];
$month = (int) $_POST['myMonth'];

if ($year > 0)
{
if ($month >= 1 && $month <=12)
{
PrintMon($year, $month);
}
else if($month != NULL)
{
echo("月份不对" . "<br />");
}
}
else if($year != NULL)
{
echo("年份不对" . "<br />");
}
?>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: