一个最简单的日历程序(php4.3.3,自己修改):
<?php
function build_calender_table($year=0,$month=0,$day=0)
{
global $PHP_SELF;
$cal="";
$year=$_GET['year'];
$month=$_GET['month']; if($year==0) $year=date('Y')+0;
if($month==0) $month=date('m')+0;
if($day==0) $day=date('d'+0);
$pyear=($month==1?$year-1:$year);
$pmonth=($month==1?12:$month-1);
$nyear=($month==12?$year+1:$year);
$nmonth=($month==12?1:$month+1);
$firstday=mktime(0,0,0,$month,1,$year);
$skip=date('w',$firstday);
$dim=date('t',$firstday);
$rows=ceil(($skip+$dim)/7);
$cal.="\n<table width=160 border=0 cellpadding=1 cellspacing=1 align=center>";
$cal.="\n<tr><td colspan=7 align=center>";
$cal.="<a href='"."$PHP_SELF?year=$pyear&month=$pmonth"."' onclick='javascript:documnet.form1.mon.value=1'>上月</a> ";
$cal.=$year.'/'.$month;
$cal.=" <a href='"."$PHP_SELF?year=$nyear&month=$nmonth"."'>下月</a>";
$cal.="\n</td></tr>";
$cal.="\n<tr align=center>";
$cal.="\n<td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td>";
$cal.="\n</tr>";
for($row=0;$row<$rows;$row++)
{
$cal.="\n<tr align=center>";
for($col=0;$col<7;$col++)
{
$cur=$row*7+$col-$skip+1;
$cal.="\n<td";
if($year=(int)date('Y')&&$month==(int)date('m')&&$cur==(int)date('d')) $cal.=" bgcolor=#d0d0d0";
$cal.=">";
if($row*7+$col<$skip) $cal.="$nbsp;";
else
{
if($cur>$dim) $cal.=" ";
else
{
if($year=(int)date('Y')&&$month==(int)date('m')&&$cur==$day) $cal.="</b>";
$cal.="<a href='"."$PHP_SELF?year=$year&month=$month&day=$cur"."'>".$cur."</a>";
if($year=(int)date('Y')&&$month==(int)date('m')&&$cur==$day) $cal.="</b>";
}
}
$cal.="</td>";
}
$cal.="\n</tr>";
}
$cal.="\n</table>";
echo $cal;
}
build_calender_table();
?>
<?php
function build_calender_table($year=0,$month=0,$day=0)
{
global $PHP_SELF;
$cal="";
$year=$_GET['year'];
$month=$_GET['month']; if($year==0) $year=date('Y')+0;
if($month==0) $month=date('m')+0;
if($day==0) $day=date('d'+0);
$pyear=($month==1?$year-1:$year);
$pmonth=($month==1?12:$month-1);
$nyear=($month==12?$year+1:$year);
$nmonth=($month==12?1:$month+1);
$firstday=mktime(0,0,0,$month,1,$year);
$skip=date('w',$firstday);
$dim=date('t',$firstday);
$rows=ceil(($skip+$dim)/7);
$cal.="\n<table width=160 border=0 cellpadding=1 cellspacing=1 align=center>";
$cal.="\n<tr><td colspan=7 align=center>";
$cal.="<a href='"."$PHP_SELF?year=$pyear&month=$pmonth"."' onclick='javascript:documnet.form1.mon.value=1'>上月</a> ";
$cal.=$year.'/'.$month;
$cal.=" <a href='"."$PHP_SELF?year=$nyear&month=$nmonth"."'>下月</a>";
$cal.="\n</td></tr>";
$cal.="\n<tr align=center>";
$cal.="\n<td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td>";
$cal.="\n</tr>";
for($row=0;$row<$rows;$row++)
{
$cal.="\n<tr align=center>";
for($col=0;$col<7;$col++)
{
$cur=$row*7+$col-$skip+1;
$cal.="\n<td";
if($year=(int)date('Y')&&$month==(int)date('m')&&$cur==(int)date('d')) $cal.=" bgcolor=#d0d0d0";
$cal.=">";
if($row*7+$col<$skip) $cal.="$nbsp;";
else
{
if($cur>$dim) $cal.=" ";
else
{
if($year=(int)date('Y')&&$month==(int)date('m')&&$cur==$day) $cal.="</b>";
$cal.="<a href='"."$PHP_SELF?year=$year&month=$month&day=$cur"."'>".$cur."</a>";
if($year=(int)date('Y')&&$month==(int)date('m')&&$cur==$day) $cal.="</b>";
}
}
$cal.="</td>";
}
$cal.="\n</tr>";
}
$cal.="\n</table>";
echo $cal;
}
build_calender_table();
?>
class Calendar{/*
* 日历
*
* @作者:sports98
* Email:[email protected]
* @版本:V1.0
*/ var $YEAR,$MONTH,$DAY;
var $WEEK=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var $_MONTH=array(
"01"=>"一月",
"02"=>"二月",
"03"=>"三月",
"04"=>"四月",
"05"=>"五月",
"06"=>"六月",
"07"=>"七月",
"08"=>"八月",
"09"=>"九月",
"10"=>"十月",
"11"=>"十一月",
"12"=>"十二月"
); //设置年份
function setYear($year){
$this->YEAR=$year;
}
//获得年份
function getYear(){
return $this->YEAR;
}
//设置月份
function setMonth($month){
$this->MONTH=$month;
}
//获得月份
function getMonth(){
return $this->MONTH;
}
//设置日期
function setDay($day){
$this->DAY=$day;
}
//获得日期
function getDay(){
return $this->DAY;
}
//打印日历
function OUT(){
$this->_env();
$week=$this->getWeek($this->YEAR,$this->MONTH,$this->DAY);//获得日期为星期几 (例如今天为2003-07-18,星期五)
$fweek=$this->getWeek($this->YEAR,$this->MONTH,1);//获得此月第一天为星期几
echo "<div style=\"margin:0;border:1 solid black;width:300;font:9pt\">
<form action=$_SERVER[PHP_SELF] method=\"post\" style=\"margin:0\">
<select name=\"month\" onchange=\"this.form.submit();\">"; for($ttmpa=1;$ttmpa<13;$ttmpa++){//打印12个月
$ttmpb=sprintf("%02d",$ttmpa);
if(strcmp($ttmpb,$this->MONTH)==0){
$select="selected style=\"background-color:#c0c0c0\"";
}else{
$select="";
}
echo "<option value=\"$ttmpb\" $select>".$this->_MONTH[$ttmpb]."</option>\r\n";
} echo " </select> <select name=\"year\" onchange=\"this.form.submit();\">";//打印年份,前后10年
for($ctmpa=$this->YEAR-10;$ctmpa<$this->YEAR+10;$ctmpa++){
if($ctmpa>2037){
break;
}
if($ctmpa<1970){
continue;
}
if(strcmp($ctmpa,$this->YEAR)==0){
$select="selected style=\"background-color:#c0c0c0\"";
}else{
$select="";
}
echo "<option value=\"$ctmpa\" $select>$ctmpa</option>\r\n";
}
echo "</select>
</form>
<table border=0 align=center>";
for($Tmpa=0;$Tmpa<count($this->WEEK);$Tmpa++){//打印星期标头
echo "<td>".$this->WEEK[$Tmpa];
}
for($Tmpb=1;$Tmpb<=date("t",mktime(0,0,0,$this->MONTH,$this->DAY,$this->YEAR));$Tmpb++){//打印所有日期
if(strcmp($Tmpb,$this->DAY)==0){ //获得当前日期,做标记
$flag=" bgcolor='#ff0000'";
}else{
$flag=' bgcolor=#ffffff';
}
if($Tmpb==1){
echo "<tr>"; //补充打印
for($Tmpc=0;$Tmpc<$fweek;$Tmpc++){
echo "<td>";
}
}
if(strcmp($this->getWeek($this->YEAR,$this->MONTH,$Tmpb),0)==0){
echo "<tr><td align=center $flag>$Tmpb";
}else{
echo "<td align=center $flag>$Tmpb";
}
}
echo "</table></div>";
}
//获得方法内指定的日期的星期数
function getWeek($year,$month,$day){
$week=date("w",mktime(0,0,0,$month,$day,$year));//获得星期
return $week;//获得星期
} function _env(){
if(isset($_POST[month])){ //有指定月
$month=$_POST[month];
}else{
$month=date("m"); //默认为本月
}
if(isset($_POST[year])){ //有指年
$year=$_POST[year];
}else{
$year=date("Y"); //默认为本年
}
$this->setYear($year);
$this->setMonth($month);
$this->setDay(date("d")); }
}$D=new Calendar;
$D->OUT();
?>
<?php
include "template.inc";
$str = "<table border>
<colgroup span=5 align=right></colgroup>
<colgroup span=2 align=right style='color:red'></colgroup>
<tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>
<!-- BEGIN Month -->
<tr>
<!-- BEGIN Date -->
<td bgcolor='{color}'>{data}</td>
<!-- END Date -->
</tr>
<!-- END Month -->
</table>
";
$mode = 7;
$n = date("t");
$w = date("w",mktime(0,0,0,date("m"),1,date("Y")));
$w = $w==0?7:$w;
$j = date("j");
$tpl = new template;
$tpl->halt_on_error = "report";
$tpl->set_var("hand",$str);
$tpl->set_block("hand","Month","row");
$tpl->set_block("Month","Date","col");
$d = $i = 1;
while($i <= $n) {
if($d < $w)
$tpl->set_var("data"," ");
else {
$tpl->set_var("color",($i==$j?"#0000FF":""));
$tpl->set_var("data",$i++);
}
$tpl->parse("col","Date",true);
if($d++ % $mode == 0) {
$tpl->parse("row","Month",true);
$tpl->set_var("col");
}
}
if(--$d % $mode > 0) {
while($d++ % $mode) {
$tpl->set_var("data"," ");
$tpl->parse("col","Date",true);
}
$tpl->parse("row","Month",true);
}
$tpl->parse("out","hand",true);
$tpl->p(out);
?>