当前位置: 首页 > 文档资料 > DoitPHP 帮助文档 >

6.21 Calendar(日历)使用说明

优质
小牛编辑
136浏览
2023-12-01

DoitPHP扩展类Calendar(日历),用于输出日历(数组),并支持标记功能。

类方法使用说明:

1、setYear($year)

设置日历的年份。 默认为当前的年份。

参数说明:
$year : 所要设置的年份

2、setMonth($month)
设置月份。 默认为当前月份。

参数说明:
$month : 所要设置的月份

3、setUsedDays($days)

设置已占用的日期。

参数说明:
$days : 已占用的日期。注:本参数为数组

4、render()

输出日历数组。

参数说明:
参数为空

举例说明:

例一、显示日历

Controller文件代码内容如下:

public function indexAction() {

$calendarObj = $this->instance('Calendar');

$data = $calendarObj->render();

$this->assign('calendar', $data);

$this->display();
}

视图文件内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例:日历</title>
</head>

<body>
<table border="0" cellspacing="1" cellpadding="0" style="border:1px solid #eee;">
<tr>
<td height="30" colspan="7" align="center">
<?php echo $calendar['year']; ?>年 <?php echo $calendar['month']; ?>月
</td>
</tr>
<tr>
<td width="30" height="30" align="center" bgcolor="#EEE">日</td>
<td width="30" align="center" bgcolor="#EEE">一</td>
<td width="30" align="center" bgcolor="#EEE">二</td>
<td width="30" align="center" bgcolor="#EEE">三</td>
<td width="30" align="center" bgcolor="#EEE">四</td>
<td width="30" align="center" bgcolor="#EEE">五</td>
<td width="30" align="center" bgcolor="#EEE">六</td>
</tr>
<?php foreach($calendar['list'] as $rows){ ?>
<tr>
<?php foreach($rows as $key => $value){ ?>
<td<?php if($key==0){ echo ' height="30"'; } ?> align="center">
<?php if($value['status']){echo $value['date'];} else {echo '';} ?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</body>
</html>

运行结果:

例二、打印日历数组

将上面例一中Controller文件的代码:

$data = $calendarObj->render();

更改为:

$data = $calendarObj->setYear(2019)->setMonth(10)->render();
$this->dump($data);

其它代码不变,调试运行下。看结果如何? 如果查看下$data的数组结构,就知道数组结构如下:

Array
(
[year] => 2019
[month] => 10
[list] => Array
(
[0] => Array
(
[0] => Array
(
[date] =>
[status] =>
)

[1] => Array
(
[date] =>
[status] =>
)

[2] => Array
(
[date] => 1
[status] => 1
)

[3] => Array
(
[date] => 2
[status] => 1
)

[4] => Array
(
[date] => 3
[status] => 1
)

[5] => Array
(
[date] => 4
[status] => 1
)

[6] => Array
(
[date] => 5
[status] => 1
)

)

[1] => Array
(
[0] => Array
(
[date] => 6
[status] => 1
)

[1] => Array
(
[date] => 7
[status] => 1
)

[2] => Array
(
[date] => 8
[status] => 1
)

[3] => Array
(
[date] => 9
[status] => 1
)

[4] => Array
(
[date] => 10
[status] => 1
)

[5] => Array
(
[date] => 11
[status] => 1
)

[6] => Array
(
[date] => 12
[status] => 1
)

)

[2] => Array
(
[0] => Array
(
[date] => 13
[status] => 1
)

[1] => Array
(
[date] => 14
[status] => 1
)

[2] => Array
(
[date] => 15
[status] => 1
)

[3] => Array
(
[date] => 16
[status] => 1
)

[4] => Array
(
[date] => 17
[status] => 1
)

[5] => Array
(
[date] => 18
[status] => 1
)

[6] => Array
(
[date] => 19
[status] => 1
)

)

[3] => Array
(
[0] => Array
(
[date] => 20
[status] => 1
)

[1] => Array
(
[date] => 21
[status] => 1
)

[2] => Array
(
[date] => 22
[status] => 1
)

[3] => Array
(
[date] => 23
[status] => 1
)

[4] => Array
(
[date] => 24
[status] => 1
)

[5] => Array
(
[date] => 25
[status] => 1
)

[6] => Array
(
[date] => 26
[status] => 1
)

)

[4] => Array
(
[0] => Array
(
[date] => 27
[status] => 1
)

[1] => Array
(
[date] => 28
[status] => 1
)

[2] => Array
(
[date] => 29
[status] => 1
)

[3] => Array
(
[date] => 30
[status] => 1
)

[4] => Array
(
[date] => 31
[status] => 1
)

[5] => Array
(
[date] =>
[status] =>
)

[6] => Array
(
[date] =>
[status] =>
)
)
)
)