一个简单的日历程序

<?php 

	class calendar{
		private $week;
		private $year;
		private $month;
		private $week_first_day;
		private $month_all_day;
		private $a;
		public function __construct(){
			$this->week = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
			$this->year = isset($_REQUEST['year'])?$_REQUEST['year']:date('Y');
			$this->month = isset($_REQUEST['month'])?$_REQUEST['month']:date('m');
			$this->a = strtotime($this->year.'-'.$this->month.'-1');
			$this->week_first_day = date('w',$this->a);
			$this->month_all_day = date('t',$this->a);
		}

		public function out(){ 
			echo '<meta charset="utf-8" />';
			echo '<title>日历实例</title>';
			echo '<link rel="shortcut icon" href="icon.jpg" type="image/x-icon" />';
			echo "<table style='width:500px;text-align:center'>";
			echo "<tr>";
			echo "<td colspan=2><a href='".$this->prevYear()."'><<上一年</a><a href='".$this->prevMonth()."'><上一月</a></td><td colspan=3><h2>日 历</h2><p>".$this->year."年".$this->month."月</p></td><td colspan=2><a href='".$this->nextMonth()."'>下一月></a><a href='".$this->nextYear()."'>下一年>></a></td>";
			echo "</tr>";
			$this->week();
			$this->day();
			$this->formPost();
			echo "</table>";
		}

		public function day(){
			echo "<tr>";
				for ($i=0; $i < $this->week_first_day; $i++) { 
					echo '<td></td>';
				}

				for ($j=0; $j < $this->month_all_day; $j++) { 
					echo '<td>'.($j+1).'日</td>';
					if(($j+1+$i)%7 == 0){ 
						echo "</tr><tr>";
					}
				}
			echo "</tr>";
		}

		public function nextYear(){ 
			$year = $this->year + 1;
			$url = "?year=".$year."&month=".$this->month."";
			return $url;
		}

		public function addYear($year){ 
			$year = $year + 1;
			return $year;
		}

		public function cutYear($year){ 
			$year = $year - 1;
			return $year;
		}

		public function nextMonth(){ 
			if( $this->month > 11 ){ 
				$month = 1;
				$url = "?year=".$this->addYear($this->year)."&month=".$month."";
				return $url;
			}
			$month = $this->month + 1;
			$url = "?year=".$this->year."&month=".$month."";
			return $url;
		}

		public function prevYear(){ 
			$year = $this->year - 1;
			$url = "?year=".$year."&month=".$this->month."";
			return $url;
		}

		public function prevMonth(){ 
			if( $this->month < 2 ){ 
				$month = 12;
				$url = "?year=".$this->cutYear($this->year)."&month=".$month."";
				return $url;
			}
			$month = $this->month - 1;
			$url = "?year=".$this->year."&month=".$month."";
			return $url;
		}

		public function week(){ 
			echo "<tr>";

				for ($i=0; $i < count($this->week); $i++) { 
					  echo '<td style="border:red 1px solid;">';
					  echo $this->week[$i];
					  echo '</td>';
				}

			echo "</tr>";
		}

		public function formPost(){ 
			echo "<tr>";
				echo "<td colspan=7><form action='' method='post' name='form'>";
				echo "<select name='year' onchange='document.form.submit()'>";
					for($i=date('Y');$i>=1970;$i--){ 
						echo "<option value='".$i."'>".$i."年</option>";						
					}
				echo "</select>";
				echo "<select name='month' onchange='document.form.submit()'>";
					for($j=1;$j<=12;$j++){ 
						echo "<option value='".$j."'>".$j."月</option>";						
					}
				echo "</select>";
				echo "</form></td>";
			echo "</tr>";
		}
	}

	$calendar = new calendar;
	$calendar->out();

 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逝者如斯乎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值