• 4500阅读
  • 0回复

PHP日历----非常简单的算法[转] [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2005-07-30
原文见:http://www.bsd8.com/html/2004-11-09/17.htm


<?php //日历
if(!$month = $_GET['month'])
$month = date("n");
if(!$year = $_GET['year'])
$year = date("Y");
$week_array = array("<font color='red'>日</font>","一","二","三","四","五","六");
?>
<head>
<STYLE>
table{font-family:Tahoma,Georgia; font-size:12px; border-collapse:collapse;}
td{border-width:1; border-color:#cccccc; border-style:solid;}
</STYLE>
</head>
<body>
<table width="210"><tr><td align="center" colspan="2" bgcolor="#bbbbff">
<?php
echo "<a href='$PHP_SELF?month=$month&year=".($year-1)."'><</a> 年 <a href='$PHP_SELF?month=$month&year=".($year+1)."'>></a>";
?>
</td><td align="center" colspan="3" bgcolor="#bbbbff">
<?=$year."年".$month."月"?>
</td><td align="center" colspan="2" bgcolor="#bbbbff">
<?php
echo ($month>1) ? "<a href='$PHP_SELF?month=".($month-1)."&year=$year'><</a>" : "<";
echo " 月 ";
echo ($month<12) ? "<a href='$PHP_SELF?month=".($month+1)."&year=$year'>></a>" : ">";
?>
</td></tr><tr>
<?php
foreach($week_array as $week)
{
echo "<td align='center'><b>".$week."</b></td>";
}
?>
</tr>
<?php
$n = 0;
$w = getWeek($month,$year) ; //获到首日周数
for($x = 0; $x < $w; $x ++)   //输出首日前表格
{
echo ($x==0) ? "<tr>" : "";
echo "<td width='30'> </td>";
$n ++;
}
for($day = 1 ; $day < 32; $day ++)   //输出日期
{
if(!checkdate($month,$day,$year))
  break;
echo ($n==0)?"<tr>":"";
$color   = ($year.$month.$day == date("Ynd"))? "yellow" : "white";
$fontColor = ($n==0)? "red" : "#888888";
echo "<td align='center' width='30' bgcolor='$color'><font color='$fontColor'>".$day."</font></td>";
$n ++;
if($n==7)
{
  echo "</tr>";
  $n = 0;
}
}
while($n<7 && $n!=0)   //输出末日后表格
{
echo "<td width='30'> </td>";
$n++;
}
?>
</table>
</body>
<?php
function getWeek($month,$year)
{
$time = mktime(0,0,0,$month,1,$year);
return date("w",$time);
}
?>
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个