php簡(jiǎn)單日歷函數(shù)
本文實(shí)例講述了php實(shí)現(xiàn)的日歷程序。分享給大家供大家參考。具體如下:
<?php
/*
* php 輸出日歷程序
*/
header("Content-type: text/html;charset=utf-8");
$year=(!isset($_GET['year'])||$_GET['year']=="")?date("Y"):$_GET['year'];
$month=(!isset($_GET['month'])||$_GET['month']=="")?date("n"):$_GET['month'];
$curUrl=$_SERVER['PHP_SELF'];
if($year<1971){
echo "出錯(cuò)!";
echo "<BR>";
echo "<a href=",$curUrl,">Back</a>";
exit();
}
?>
<table width="200" border="1" cellspacing="0" cellpadding="0" bordercolor="#E7E7E7" style="font-size:12px;" align="center">
<tr align="center"><td colspan="2">
<?php
//<-------當(dāng)月份超出1至12時(shí)的處理;開(kāi)始------->
if($month<1){
$month=12;
$year-=1;
}
if($month>12){
$month=1;
$year+=1;
}
//<-------當(dāng)月份超出1至12時(shí)的處理;結(jié)束------->
//<---------上一年,下一年,上月,下月的連接處理及輸出;開(kāi)始--------->
echo "<a href=",$curUrl,"?year=".($year-1)."&month=".$month."><<</a>年<a href=",$curUrl,"?year=".($year+1)."&month=".$month.">>></a>";
?>
</td><td colspan="3"><?php echo $year."年".$month."月";?>
</td><td colspan="2">
<?php
echo "<a href=",$curUrl,"?month=".($month-1)."&year=".$year."><<</a>月<a href=",$curUrl,"?month=".($month+1)."&year=".$year.">>></a>";
//<--------上一年,下一年,上月,下月的連接處理及輸出;結(jié)束--------->
?></td></tr>
<tr align=center><td><font color="red">日</font></td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr><tr>
<?php
$d=date("d");
$FirstDay=date("w",mktime(0,0,0,$month,1,$year));//取得任何一個(gè)月的一號(hào)是星期幾,用于計(jì)算一號(hào)是由表格的第幾格開(kāi)始
$bgtoday=date("d");
function font_color($month,$today,$year){//用于計(jì)算星期天的字體顏色
$sunday=date("w",mktime(0,0,0,$month,$today,$year));
if($sunday=="0"){
$FontColor="red";
}else{
$FontColor="black";
}
return $FontColor;
}
function bgcolor($month,$bgtoday,$today_i,$year){//用于計(jì)算當(dāng)日的背景顏色
$show_today=date("d",mktime(0,0,0,$month,$today_i,$year));
$sys_today=date("d",mktime(0,0,0,$month,$bgtoday,$year));
if($show_today==$sys_today){
$bgcolor="bgcolor=#6699FF";
}else{
$bgcolor="";
}
return $bgcolor;
}
function font_style($month,$today,$year){//用于計(jì)算星期天的字體風(fēng)格
$sunday=date("w",mktime(0,0,0,$month,$today,$year));
if($sunday=="0"){
$FontStyle="<strong>";
}else{
$FontStyle="";
}
return $FontStyle;
}
for($i=0;$i<=$FirstDay;$i++){//此for用于輸出某個(gè)月的一號(hào)位置
for($i;$i<$FirstDay;$i++){
echo "<td align=center> </td>\n";
}
if($i==$FirstDay){
echo "<td align=center ".bgcolor($month,$bgtoday,1,$year)."><font color=".font_color($month,1,$year).">".font_style($month,1,$year)."1</font></td>\n";
if($FirstDay==6){//判斷1號(hào)是否星期六
echo "</tr>";
}
}
}
$countMonth=date("t",mktime(0,0,0,$month,1,$year));//某月的總天數(shù)
for($i=2;$i<=$countMonth;$i++){//輸出由1號(hào)定位,隨后2號(hào)直至月尾的所有號(hào)數(shù)
echo "<td align=center ".bgcolor($month,$bgtoday,$i,$year)."><font color=".font_color($month,$i,$year).">".font_style($month,$i,$year)."$i</font></td>\n";
if(date("w",mktime(0,0,0,$month,$i,$year))==6){//判斷該日是否星期六
echo "</tr>\n";
}
}
?>
</table>
php輸出的日歷效果如下圖:

希望本文對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
相關(guān)文章
php中file_get_content 和curl以及fopen 效率分析
關(guān)于file_get_content 和curl以及fopen 的效率問(wèn)題,小編比較傾向于使用curl來(lái)訪問(wèn)遠(yuǎn)程url。Php有curl模塊擴(kuò)展,功能很是強(qiáng)大。沒(méi)事可以研究一下。2014-09-09
PHP+iFrame實(shí)現(xiàn)頁(yè)面無(wú)需刷新的異步文件上傳
這篇文章主要介紹了PHP+iFrame實(shí)現(xiàn)頁(yè)面無(wú)需刷新的異步文件上傳,包含了iframe框架與form表單的運(yùn)用及PHP文件上傳等技巧,需要的朋友可以參考下2014-09-09
PHPExcel凍結(jié)(鎖定)表頭的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了PHPExcel凍結(jié)(鎖定)表頭的簡(jiǎn)單實(shí)現(xiàn)方法,涉及使用PHPExcel針對(duì)表格屬性的簡(jiǎn)單操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2017-09-09
php小技巧 把數(shù)組的鍵和值交換形成了新的數(shù)組,查找值取得鍵
php小技巧--把數(shù)組的鍵和值交換形成了新的數(shù)組,查找值取得鍵的實(shí)現(xiàn)方法。2011-06-06
PHP采集相關(guān)教程之一 CURL函數(shù)庫(kù)
目前為目最全的CURL中文說(shuō)明了,學(xué)PHP的要好好掌握。有很多的參數(shù)。大部份都很有用。真正掌握了它和正則,一定就是個(gè)采集高手了。2010-02-02
PHP錯(cuò)誤提示It is not safe to rely on the system……的解決方法
今天小編就為大家分享一篇關(guān)于PHP錯(cuò)誤提示It is not safe to rely on the system……的解決方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03

