php簡單日歷函數(shù)
更新時間:2015年10月28日 15:42:56 投稿:lijiao
這篇文章主要介紹了php簡單日歷函數(shù),沒有選擇比較常見的用js生成的日歷,而是用php輸出了一個日歷表格,感興趣的小伙伴們可以參考一下
本文實例講述了php實現(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 "出錯!";
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時的處理;開始------->
if($month<1){
$month=12;
$year-=1;
}
if($month>12){
$month=1;
$year+=1;
}
//<-------當(dāng)月份超出1至12時的處理;結(jié)束------->
//<---------上一年,下一年,上月,下月的連接處理及輸出;開始--------->
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));//取得任何一個月的一號是星期幾,用于計算一號是由表格的第幾格開始
$bgtoday=date("d");
function font_color($month,$today,$year){//用于計算星期天的字體顏色
$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){//用于計算當(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){//用于計算星期天的字體風(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用于輸出某個月的一號位置
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號是否星期六
echo "</tr>";
}
}
}
$countMonth=date("t",mktime(0,0,0,$month,1,$year));//某月的總天數(shù)
for($i=2;$i<=$countMonth;$i++){//輸出由1號定位,隨后2號直至月尾的所有號數(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輸出的日歷效果如下圖:

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

