用PHP寫的DIV+CSS日曆函數

 其實這篇文章也不能算作是原創,只是在原作者的基礎上修改的比較多,幾乎重寫了一遍。

<?php
 /**
*在原作者的基礎上修改的,呵呵,不過忘記了原作者是誰了,
*如果原作者看到的話,給我一個提示,o(∩_∩)o...
*我的blog:http://etongchina.blogcn.com
*我的郵箱:[email protected]
**/
function calendar($year='',$month='',$linkurl='',$checkurl=''){
//處理日期
 $year = $year?$year:date("Y");
 $month = ($month!=='')?$month:date("m");
 $day = $day?$day:date("d");
 
// if(date("Y-m") < ($year.'-'.$month)){
//  showmessage("查詢日期大於當前日期,請檢查輸入");
// }
 if($year<2000){
  showmessage("日期出錯了,請檢查輸入",$_SESSION[url_forward],1);
 }
 if($month==0) {
     $month=12; $year--;
 }
 if($month==13) {
     $month=1; $year++;
 }
 $str = '';
 $str .='<div style="width:300px; margin-top:50px;">';
 $str .= '<div style="float:left">';
 $str .= '<span class="calendar" style="width:30px;height:28px;">';
 $str .= '<a href="javascript:getResponse('."'".$linkurl."&month=".($month-1)."&year=".$year."'".')">'."<<</a></span>";
 $str .= '<span class="calendar" style="width:158px; height:28px;font-size:12px">';
 $str .="<a><b>".$year."年".$month."月</b></a></span>";
 $str .='<span class="calendar" style="width:30px;height:28px;">';
 $str .= '<a href="javascript:getResponse('."'".$linkurl."&month=".($month+1)."&year=".$year."'".')">'.">></a></span>";
 $str .= '<span class="calendar" style="width:60px;height:28px; line-height:27px;">生成圖表</span>';
 $str .='</div>';
 $str .='
 <div style="clear:both">
 <span class="calendar" style="color:#FF0000">日</span>
 <span class="calendar">一</span>
 <span class="calendar">二</span>
 <span class="calendar">三</span>
 <span class="calendar">四</span>
 <span class="calendar">五</span>
 <span class="calendar">六</span>';
 $str .= '<span class="calendar" style="width:60px;height:25px;line-height:25px;"></span>';
 $str .= '</div><div style="clear:both;">';
  
 $d=date("d");
 $countLastMonth=date("t",mktime(0,0,0,($month-1),1,$year));//上個月總天數
 $FirstDay=date("w",mktime(0,0,0,$month,1,$year));//取得任何一個月的一號是星期幾,用於計算一號是由表格的第幾格開始
 $bgtoday=date("d");

 if($FirstDay!=0){  //判斷1號是否星期日
  for($i=($countLastMonth-$FirstDay+1);$i<=$countLastMonth;$i++){  //此for用於輸出某個月的一號前位置
  $str .='<span class="calendar" style="color:#CCCCCC">'.$i.'</span>';
  }
  for($i=1;$i<=7-$FirstDay;$i++){  //此for用於輸出某個月的一號位置
  $str .='<span class="calendar">'.$i.'</span>'; 
  }  
 }else{
  for($i=1;$i<=7;$i++){  //此for用於輸出某個月的一號前位置
  $str .='<span class="calendar">'.$i.'</span>';
  }
  
 }
 $str .= '<span class="calendar" style="width:60px;height:25px;line-height:25px;">';
 $str .= '<a href="javascript:getResponse('."'".$checkurl."&start=".$year.'- '.$month.'-'.(7-$FirstDay)."'".')">'."查詢本週</a></span>";
 $str .= '</div><div style="clear:both;">';
 
 $countMonth=date("t",mktime(0,0,0,$month,1,$year));//某月的總天數
 for($i=7-$FirstDay+1;$i<=$countMonth;$i++){ //輸出由1號定位,隨後的X(週日)號直至月尾的所有號數
  $str .= "<span class='calendar' style='".bgcolor($month,$bgtoday,$i,$year)."'>".($i+$j)."</span>";
  if( ($i+$FirstDay)%7 == 0){
   $str .= '<span class="calendar" style="width:60px;height:25px;line-height:25px;">';
   $str .= '<a href="javascript:getResponse('."'".$checkurl."&start=".$year.'- '.$month.'-'.$i."'".')">'."查詢本週</a></span>";
   $str.='</div><div style="clear:both;">';
  }
 }
 $LastDay=date("w",mktime(0,0,0,$month,$countMonth,$year));//取得任何一個月的一號是星期幾,用於計算一號是由表格的第幾格開
 if($LastDay!=6){
  for($i=1;$i<7-$LastDay;$i++){  //此for用於輸出某個月的一號前位置
   $str .='<span class="calendar" style="color:#CCCCCC">'.$i.'</span>';
  }
  $str .= '<span class="calendar" style="width:60px;height:25px;line-height:25px;">';
  $str .= '<a href="javascript:getResponse('."'".$checkurl."&start=".$year.'-'. ($month+1).'-'.(7-$LastDay+1)."'".')">'."查詢本週</a>< /span>";  
 }
 $str .= '</div>';  
 return $str;
}

/***用於計算當日的背景顏色***/
function bgcolor($month,$bgtoday,$today_i,$year)
{
    $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=" background-color: #6699FF";
    }else{
        $bgcolor="";
    }
    return $bgcolor;

?>

發佈了25 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章