<?php
/**
* @Name ExampleCalendar.php
* @Desc Simple canendar program
*
* @Version 1.1
* @Author Bian Yuan
* @Email [email protected]
* @Since 2009/04/30
*/
session_start();
function html_select($name, $options=array(), $firstValue="Please Select", $default="", $onchange=""){
if(empty($onchange)){
$select = "<select name='".$name."' id='".$name."'>";
}else{
$select = "<select name='".$name."' id='".$name."' onchange=\"".$onchange."\" >";
}
if($firstValue != ''){
$select .= "<option value='-1'>".$firstValue."</option>";
}
foreach($options as $k=>$v){
if($default == $v){
$select .= "<option value='".$k."' selected='selected'>$v</option>";
}else{
$select .= "<option value='".$k."'>$v</option>";
}
}
$select .= "</select>";
return $select;
}
class Calendar
{
private $year;
private $month;
private $week = array(
'Sunday' => '星期日',
'Monday' => '星期一',
'Tuesday' => '星期二',
'Wednesday' => '星期三',
'Thursday' => '星期四',
'Friday' => '星期五',
'Saturday' => '星期六'
);
private $data = array(
);
/**
* @desc Calendar构造方法
**/
public function __construct($year='', $month='')
{
$this->year = $year == '' ? date('Y') : intval($year);
$this->month = $month == '' ? date('n') : intval($month);
}
public function setData($info=array())
{
$this->data = $info;
}
/**
* @desc 获得这个月的第一天是星期几
**/
public function getFirstDayOfMonth()
{
return date("w", mktime(0, 0, 0, $this->month, 1, $this->year));
}
/**
* @desc 获得这个月有多少天
**/
public function getDaysInMonth()
{
return date("t", mktime(0, 0, 0, $this->month, 1, $this->year));
}
public function getCalendar()
{
$s = "<table style='font-size:12px;' id='calendar' width='100%' border='1' cellpadding='0' cellspacing='0'>\n";
$s .= "<thead>\n<tr>\n";
foreach ($this->week as $item)
{
$s .= "<td>{$item}</td>\n";
}
$s .= "</tr>\n</thead>\n<tbody>\n<tr>\n";
$t = $this->getDaysInMonth();
$nowDay = date('Ynd');
for($day=1; $day<=$t; $day++)
{
if(array_key_exists($this->year ."-". $this->month . "-" . $day, $this->data))
{
$info = '<ul>';
if(! empty($this->data[$this->year ."-". $this->month . "-" . $day]))
{
foreach ($this->data[$this->year ."-". $this->month . "-" . $day] as $item)
{
$info .= "<li>{$item}</li>";
}
}
$info .= '</ul>';
}
// Set day bgcolor
if($this->year.$this->month.$day == $nowDay)
{
$dayBgColor = 'blue';
}else{
$dayBgColor = 'yellow';
}
if(1 == $day)
{
$firstDayOfMonth = $this->getFirstDayOfMonth();
$s .= str_repeat("<td> </td>\n", $firstDayOfMonth);
}
$day_in_week_pos = $w = date("w",mktime(0,0,0,$this->month, $day, $this->year));
if(6 == $w)
{
$s .= "<td bgcolor='".$dayBgColor."'>{$day}{$info}</td>\n</tr>\n";
}else{
$s .= "<td bgcolor='".$dayBgColor."'>{$day}{$info}</td>\n";
}
unset($info);
}
$s .= str_repeat("<td> </td>\n", 6-$day_in_week_pos);
$s .= "</tr></tbody>\n</table>\n";
return $s;
}
}
// Make year select options
for($year=2007; $year <=2020; $year++) {
$yearOption[$year] = $year;
}
// Make month select options
for($month=1; $month<=12; $month++){
$monthOption[$month] = $month;
}
$year = isset($_GET['year']) ? intval($_GET['year']) : date('Y');
$month = isset($_GET['month']) ? intval($_GET['month']) : date('n');
$yearOnChange = "window.location.href='?year='+this.value+'&month={$month}'";
$monthOnChange = "window.location.href='?month='+this.value+'&year={$year}'";
echo html_select('year', $yearOption, '', $year, $yearOnChange);
echo html_select('month', $monthOption, '', $month, $monthOnChange);
if(isset($_POST['submit']))
{
$y = intval($_POST['y']);
$m = intval($_POST['m']);
$d = intval($_POST['d']);
if(!checkdate($m,$d,$y))
{
die('error date');
}
$date = $y.'-'.$m.'-'.$d;
$_SESSION['data'][$date][] = strip_tags($_POST['info']);
}
$calendar = new Calendar($year, $month);
if(!isset($_SESSION['data']))
{
$_SESSION['data'] = array(
'2009-4-28' => array('有些事吧。。。','今天几号?','事情三'),
'2009-4-29' => array('反正就是闹心','事情二'),
'2009-4-30' => array('按照自己的想法写,愿咋写怎么写。')
);
}
$calendar->setData($_SESSION['data']);
echo $calendar->getCalendar();
?>
<form method="post" action="">
<input name="y" type="text" />年
<input name="m" type="text" />月
<input name="d" type="text" />日
<input name="info" type="text" />事情
<input type="submit" name="submit">
</form>
|