首頁 > 後端開發 > php教程 > php日历

php日历

WBOY
發布: 2016-06-23 13:26:05
原創
1437 人瀏覽過

<?phpclass Calendar {	private $year;	private $month;	private $val;	private $weeks = array ('日', '一', '二', '三', '四', '五', '六' );		function __construct($options = array(), $val) {		$this->year = date ( 'Y' );		$this->month = date ( 'm' );		$this->val = $val;				$vars = get_class_vars ( get_class ( $this ) );		foreach ( $options as $key => $value ) {			if (array_key_exists ( $key, $vars )) {				$this->$key = $value;			}		}	}		function display() {		$value .= '<table  class="calendar">';		$value .= $this->showChangeDate ();		$value .= $this->showWeeks ();		$value .= $this->showDays ( $this->year, $this->month );		$value .= '</table>';		return $value;	}		private function showWeeks() {		$value .= '<tr>';		foreach ( $this->weeks as $title ) {			$value .= '<th>' . $title . '</th>';		}		$value .= '</tr>';		return $value;	}		private function showDays($year, $month) {		 $nowtime = mktime(0,0,0,$month,1,$year);//当月1号转为秒 	     $daysofmonth = date(t,$nowtime);//当月天数 	     $weekofbeginday = date(w,$nowtime);//当月第一天是星期几 	     $weekofendday = date(w,mktime(0,0,0,$month+1,0,$year));//当月最后一天是星期几 	     $daysofprevmonth = date(t,mktime(0,0,0,$month,0,$year));//上个月天数 		 $count = 1;//计数     	 //列出上月后几天    		 for($i = 1 ; $i <= $weekofbeginday ; $i++)          {              $value .= "<td>".($daysofprevmonth-$weekofbeginday+$i)."</td>";              $count++;          } 	     //当月全部 	     for($i = 1 ; $i <= $daysofmonth ; $i++) 	     { 	         $css = ($count%7==0 || $count%7==1)?"weekday":"normalday"; 	         if($i == $today) $css .= "today"; 	         $value .= "<td class='".$css."'>".$i."</td>"; 	         if($count%7==0)  $value .= "</tr><tr>"; 	         $count++; 	     }	     //下月前几天     	 for ($i = 1;$i <= 6-$weekofendday;$i++)          {             $value .= "<td class='othermonth'>".$i."</td>";          } 				 $value .= '</tr>';		 return $value;	}		private function showChangeDate() {				$url = basename ( $_SERVER ['PHP_SELF'] );				$value = '<tr>';		$value .= '<td><a href="?' . $this->preYearUrl ( $this->year, $this->month ) . '">' . '上年' . '</a></td>';		$value .= '<td><a href="?' . $this->preMonthUrl ( $this->year, $this->month ) . '">' . '上月' . '</a></td>';		$value .= '<td colspan="3"><form>';				$value .= '<select name="year" onchange="window.location=\'' . $url . '?year=\'+this.options[selectedIndex].value+\'&month=' . $this->month . '\'">';		for($ye = 1970; $ye <= 2038; $ye ++) {			$selected = ($ye == $this->year) ? 'selected' : '';			$value .= '<option ' . $selected . ' value="' . $ye . '">' . $ye . '</option>';		}		$value .= '</select>';		$value .= '<select name="month" onchange="window.location=\'' . $url . '?year=' . $this->year . '&month=\'+this.options[selectedIndex].value+\'\'">';				for($mo = 1; $mo <= 12; $mo ++) {			$selected = ($mo == $this->month) ? 'selected' : '';			$value .= '<option ' . $selected . ' value="' . $mo . '">' . $mo . '</option>';		}		$value .= '</select>';		$value .= '</form></td>';		$value .= '<td><a href="?' . $this->nextMonthUrl ( $this->year, $this->month ) . '">' . '下月' . '</a></td>';		$value .= '<td><a href="?' . $this->nextYearUrl ( $this->year, $this->month ) . '">' . '下年' . '</a></td>';		$value .= '</tr>';		return $value;	}		private function preYearUrl($year, $month) {		$year = ($this->year <= 1970) ? 1970 : $year - 1;				return 'year=' . $year . '&month=' . $month;	}		private function nextYearUrl($year, $month) {		$year = ($year >= 2038) ? 2038 : $year + 1;				return 'year=' . $year . '&month=' . $month;	}		private function preMonthUrl($year, $month) {		if ($month == 1) {			$month = 12;			$year = ($year <= 1970) ? 1970 : $year - 1;		} else {			$month --;		}				return 'year=' . $year . '&month=' . $month;	}		private function nextMonthUrl($year, $month) {		if ($month == 12) {			$month = 1;			$year = ($year >= 2038) ? 2038 : $year + 1;		} else {			$month ++;		}		return 'year=' . $year . '&month=' . $month;	}}
登入後複製

<?php//获取日历$year = date ( "Y" );$month = date ( "m" );$params = array ('year' => $year, 'month' => $month );if (isset ( $_GET ['year'] ) && isset ( $_GET ['month'] )) {	$params = array ('year' => $_GET ['year'], 'month' => $_GET ['month'] );	$year = $_GET ['year'];	$month = $_GET ['month'];}$cal = new Calendar ( $params, $list_sign );echo $cal->display ();?>
登入後複製


 

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板