1.
<?php
$mnow=(isset($HTTP_GET_VARS['month']) && intval($HTTP_GET_VARS['month'])>0 && intval($HTTP_GET_VARS['month'])<13)?intval($HTTP_GET_VARS['month']):date("m");
$ynow=(isset($HTTP_GET_VARS['year']) && intval($HTTP_GET_VARS['year'])>1969 && intval($HTTP_GET_VARS['year'])<2038)?intval($HTTP_GET_VARS['year']):date("Y");
$mtime=mktime(0,0,0,$mnow,date("d"),$ynow);
$f=date("w",mktime(0,0,0,$mnow,1,$ynow))-1;
echo "<table id=\"calendar\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\">";
echo "<tr><td colspan=\"4\" align=\"center\" class=\"calendartitle\"><a href=\"?".($mnow!=date("m")?"month=".intval($mnow)."&":"")."year=".(intval($ynow)==1970?"1970":intval($ynow)-1)."\"><</a>".$ynow."<a href=\"?".($mnow!=date("m")?"month=".intval($mnow)."&":"")."year=".(intval($ynow)==2037?"2037":intval($ynow)+1)."\">></a></td><td colspan=\"3\" align=\"center\" class=\"calendartitle\"><a href=\"?month=".(intval($mnow)==1?"12":intval($mnow)-1).($ynow!=date("Y")?"&year=".intval($ynow):"")."\"><</a>".date("M",$mtime)."<a href=\"?month=".(intval($mnow)==12?"1":intval($mnow)+1).($ynow!=date("Y")?"&year=".intval($ynow):"")."\">></a></td></tr>";
echo "<tr><td class=\"calendartop\">S</td><td class=\"calendartop\">M</td><td class=\"calendartop\">T</td><td class=\"calendartop\">W</td><td class=\"calendartop\">T</td><td class=\"calendartop\">F</td><td class=\"calendartop\">S</td></tr>";
for($i=0;$i<date("t",$mtime)+$f+1;$i++){
if($i%7==0)echo "<tr>";
echo "<td".(($i-$f==intval(date("d")) && $mnow==date("m") && $ynow==date("Y"))?" id=\"calendartoday\"":"").(($i%7==0 || $i%7==6)?" class=\"calendarw\"":"").">";
if($i>$f)echo $i-$f;
echo "</td>";
if($i%7==6)echo "</tr>";
}
if($i%7<6 && $i%7>0)echo "<td colspan=\"".(7-$i%7)."\"></td></tr>";
if($i%7==6)echo "<td></td></tr>";
echo "</table>";
?>
2.
利用PHP中的Image函数实现动态生成GIF图象文件的,用户在不同的时间访问可以返回不同的小日历图片,代码如下:
<?php
header("Content-type: image/gif");
$y=date(Y);
$m=date(n);
$w=date(w);
$d=date(j);
$ws[0]="Sun";
$ws[1]="Mon";
$ws[2]="Tue";
$ws[3]="Wed";
$ws[4]="Thu";
$ws[5]="Fri";
$ws[6]="Sat";
$image = imagecreate(80,100);
if ($w==0 ||$w==6) {
$colorW= imagecolorallocate($image, 225, 0, 0);
}
else{
$colorW= imagecolorallocate($image, 0, 0, 255);
}
$colorbk = imagecolorallocate($image, 0,0,120);
$colorGreen = imagecolorallocate($image, 0, 225, 0);
$colorB = imagecolorallocate($image, 255,255,255);
$colorBlack= imagecolorallocate($image, 0,0,0);
imagefill($image, 0, 0, $colorbk);
imagefilledrectangle($image, 10,10,70,90, $colorB);
imagestring($image, 5, 23, 15,$y,$colorBlack);
imagestring($image, 5, 36, 35,$m, $colorGreen);
imagestring($image, 5, 32, 55,$d, $colorW);
imagestring($image, 4, 30, 75,$ws[$w], $colorW);
imagegif($image);
说明:您的PHP必须有支持GIF图象的GD Library,如果您现在的PHP不支持,请去下载以前GD Library
3.
PHP下的日历实现主要是依靠PHP强大的时间,日期函数.包括date()和mktime(),自己现在发现PHP下的最BT的函数莫过于date (),晕~~自己以前一直以为是那堆数据操作和文件管理的函数.实现日历功能,主要要先弄清楚它的原理,这样写起程序就简单了.
可能一开始会觉得日历很复杂,主要是每个月的日子对应的星期都有变化,还有闰年等。但仔细分析,日历的实现其实不算太复杂.一个日历最基本的肯定是顶部的 星期标识,为了编程方便和同国际习惯同步,我们把周日做为一周的起点,周六做为终点。这个实现比较简单,一个数组用循环输出就可以了。剩下的主要问题就是 日期的显示了。
日期的显示主要要抓住几个问题:
1. 这个月有多少天?
2. 这个月第一天是星期几?
至于一个月有多少周,这个基本不用考虑,因为我们在日历日期显示中对新的一周(即一般要新起一行,可以找个日历先研究一下,总不能31天全部在一行输出 吧)的处理完全不需要用到每月周数,只需要判断某一天是否是周日就可以了,再加上点html的机巧,在是周日的日期显示前先输出一个tr起新行,这样比用 周数判断方便多了.
下面就只剩前两个问题.第一个问题的意义自然不用说了,其实获得这个数据很简单,通过调用date()和mktime()函数就可以实现.可以使用下面的调用:
date("t",mktime(0,0,0,$month,1,$year));
在这里,mktime(0,0,0,$month,$day,$year)前三个0标识小时,分钟和秒,一般较少用到,直接填0,后边三个参数是标识月, 日,年.这个函数返回的是一个UNIX格式的时间戳,这个基本没什么直接意义,需要用date()函数继续处理.date();函数有很多格式化标识,就 是"t"那个...具体可以参考PHP的文档.date("t",mktime(0,0,0,$ month,$day,$year)返回的就是 mktime()返回的时间戳标识的一个月的日期数,范围从28-31.注意,这里必须提供一个具体日期,比如1号.
至于从第二个问题也是十分重要,因为每个月1号的星期数不是都一样的。所以日历输出是在每个月1号之前必须输出空格,保证每个月1号的星期数是真确的(比 如某月1号是星期4则我必须在周日到周三对应位置都输出空格).这个数据也是用date()和mktime()函数获得:
date("w",mktime(0,0,0,$month,1,$year))
这组函数返回的是每月1号的星期数,返回值从0到6依此表示周日到周六.
这样,一个日历所需要的基本数据就齐备了,可以着手实现日历.
首先是获取月份和年份,这个东西用html表单有许多不同的实现方法,我采用了输入框,这样比较简单,然后form的action是$SCRIPT_NAME,即脚本本身.方法是post.
在得到月份和年份后,就可以输出日历主要表格部分了,首先可以用一个数组输出星期数,从Sun.到Sat.然后开始输出全部的日期,这是一个循环:
for($day=1;$day<=date("t",mktime(0,0,0,$month,1,$year));$day++)
{....}
大括号里包括几个条件判断.首先是获得月份第一天的星期数,通过一个循环,在之前的个星期数对应位置前输出空格.其次一个判断是判断某天是否是周日,如果是的话要输出一个tr换行.
[hooto_center][hooto_img]upload_file/pic/200541115012.jpg[/hooto_img][/hooto_center]
这样,最后结束表格,一个简单的日历就可以输出了,稍加修改就可以用在blog或留言本里,试试看..^^
特别感谢:flyruns@hotmail.com
附加源码:
<html>
<?php
/*Calendar,By yangchengkai@gmail.com*/
/*2005-2-21*/
/*I\'ll write my own calendar here*/
$week=array("Su","Mo","Tu","We","Th","Fr","Sa");
$this_year=date("Y");
$this_month=date("n");
$this_day=date("j");
?>
<div align="center">
<form action="<?php echo $SCRIPT_NAME ?>" method="post">
Year:<input type="text" name="s_year" maxlength="4" size="5" value=<?php echo $this_year;?>>
Month:<input type="text" name="s_month" maxlength="2" size="3" value=<?php echo $this_month;?>>
<input type="submit" name="ok" value="Go!">
</form>
<?php
$year=$_POST[s_year];
$month=$_POST[s_month];
echo $year."-".$month;
?>
<table width="20%" height="160" border="1">
<?php
/*Print the calendar*/
/*we need to know what the date of the first day of selected month is.*/
/*print a whole week head*/
for ($wa=0;$wa<=6;$wa++)
{
?>
<td><font color="#000080" ><?php echo $week[$wa];?></font></td>
<?php
}
/*print all days*/
for($day=1;$day<=date("t",mktime(0,0,0,$month,1,$year)/*get the days number of this month*/);$day++)
{
if ($day == 1)
{
/*form begin*/
?>
<tr>
<?php
for ($bk=0;$bk<date("w",mktime(0,0,0,$month,1,$year));$bk++)/*the first day of the month to a certain date,print blank before it*/
{
?>
<td></td>
<?php
}
}
if ( strcmp(date("w",mktime(0,0,0,$month,$day,$year)),0) == 0)
{
/*if the day is Sun. ,then start a new line*/
?>
<tr>
<td align=center><font color="#000080" ><a href="" target=""><?php echo $day;?></a></font></td>
<?php
}
else
{
?>
<td align=center><font color="#000080" ><a href="" target=""><?php echo $day;?></a></font></td>
<?php
}
}
?>
</table>
</div>
</html>
4.
<?php
$daysofmonth=array(31,28,31,30,31,30,31,31,30,31,30,31);
$daysofmonthly=array(31,29,31,30,31,30,31,31,30,31,30,31);
$dofw=array(日,一,二,三,四,五,六);
$year=(int)isset($_get[year])?$_get[year]:date(y);
$month=(int)isset($_get[month])?$_get[month]:date(m);
$day=(int)isset($_get[day])?$_get[day]:date(d);
$cyear=$year;
//在windows系统中函数string date(string format[,int timestamp])范围限制为从1970年1月1日到2038年1月19日。
//日历循环周期为28年,以下两个循环进行范围调整
while($cyear<1971) $cyear+=28;
while($cyear>2037) $cyear-=28;
$week=(int)date(w,strtotime($cyear*100+$month.01));
if ((($year%4==0)&&($year%100!=0))||($year%400==0))
$dayofmonth=$daysofmonthly[$month-1];
else
$dayofmonth=$daysofmonth[$month-1];
?>
<html>
<head>
<title>日历</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta http-equiv="author" content="huangjian">
<style type="text/css"><!--
body,th,td,select {
font-family: 宋体;
font-size: 9pt;
color:#003399;
}
a:link {
color:#003399;
text-decoration:none;
}
a:visited {
color:#003399;
text-decoration:none;
}
a:hover {
color:#ffa500;
text-decoration:none;
}
--></style>
<script language="javascript"><!--
function calday(datestr)
{
if(opener!=null)
{
opener.restart(datestr)
self.close();
}
}
window.moveto((screen.width-240)/2,(screen.height-240)/2);
window.resizeto(240,220);
//--></script>
</head>
<body>
<form name="form_cal" action="#" method="get">
<table border="1" cellpadding="2" cellspacing="0" width="200" align="center">
<tr bgcolor="#cceeff">
<td colspan="7" align="right">
<a href="cal.php" title="转到今天">日历</a> <select name="year" onchange="javascript:form_cal.submit()">
<?php
for($i=1901;$i<=2100;$i++)
printf("<option value=\"%d\" %s >%d</option>\n",$i,$i==$year?selected:,$i);
echo </select>年 <select name="month" onchange="javascript:form_cal.submit()">;
for($i=1;$i<=12;$i++)
printf("<option value=\"%02d\"%s>%02d</option>\n",$i,$i==$month? selected :,$i);
echo </select>月</td></tr><tr align="center">;
for($i=0;$i<7;$i++)
echo <th>.$dofw[$i].</th>;
echo </tr><tr align="center">;
for($i=0;$i<$week;$i++)
echo <td> </td>;
for($day=1;$day<=$dayofmonth;$day++)
{
echo <td;
if($day==$day) echo bgcolor="#cceeff";
printf("><a href=\"javascript:calday(%d-%02d-%02d)\">%d</a></td>\n",$year,$month,$day,$day);
if(($day+$week)%7==0&&$day!=$dayofmonth) echo </tr><tr align="center">;
}
for($day--;($day+$week)%7!=0;$day++)
echo <td> </td>;
?>
</tr></table></form></body></html>

Leave a comment