在做很多程序时,需要日历选择功能,例如预约排班功能;
/**
* php生成日历功能
*/
class Calendar{
private $year;
private $month;
private $weeks = array('日','一','二','三','四','五','六');
function __construct($options = array()) {
$this->year = date('Y');
$this->month = date('m');
$vars = get_class_vars(get_class($this));
foreach ($options as $key=>$value) {
if (array_key_exists($key, $vars)) {
$this->$key = $value;
}
}
}
function display()
{
echo '<table class="calendar">';
//$this->showChangeDate();
$this->showWeeks();
$this->showDays($this->year,$this->month);
echo '</table>';
}
public function showWeeks()
{
$str = '<tr>';
foreach($this->weeks as $title)
{
$str.= '<th>'.$title.'</th>';
}
$str.= '</tr>';
return $str;
}
public function showDays($is)
{
$year = $this->year;
$month = $this->month;
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$starDay = date('w', $firstDay);
$days = date('t', $firstDay);
$str = '<tr>';
for ($i=0; $i<$starDay; $i++) {
$str.= '<td> </td>';
}
for ($j=1; $j<=$days; $j++) {
$i++;
if ($j == date('d')) {
if(in_array($j,$is)){
$class_ss = 'qiandao_2';
}else{
$class_ss = '';
}
$str.= '<td onclick="ckcal(this)" data-time="'.$year.'-'.$month.'-'.$j.'" class="'.$class_ss.'"><input type="hidden" value="" name="data[day][]"/>'.$j.'</td>';
} else {
if(in_array($j,$is)){
$class_ss = 'qiandao_1';
}else{
$class_ss = '';
}
$str.= '<td onclick="ckcal(this)" data-time="'.$year.'-'.$month.'-'.$j.'" class="'.$class_ss.'"><input type="hidden" value="" name="data[day][]"/>'.$j.'</td>';
}
if ($i % 7 == 0) {
$str.= '</tr><tr>';
}
}
$str.= '</tr>';
return $str;
}
}