-
Notifications
You must be signed in to change notification settings - Fork 6
/
calender.php
executable file
·110 lines (102 loc) · 2.91 KB
/
calender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* 月間カレンダーの表を生成して返す
* @param int $first その月の1日の曜日番号。日=0、月=1、火=2、水=3、木=4、金=5、土=6
* @param int $days その月の日数。30、31、28もしくは29
* @return array 下記のような月間カレンダーを配列で返す
* - 週ごとにひとつの配列にする。
* - 週の配列は日曜日から土曜日の7要素が必ずある。
* - 週の配列の中身は日付。
* - 週の配列の中で中で前月や次月の日付になる日は0で埋める。
* 例:
* $calendar = getCalendarTable(1, 30);
* この場合、$calendarの値は次のようになる:
* [
* [ 0, 1, 2, 3, 4, 5, 6],
* [ 7, 8, 9, 10, 11, 12, 13],
* [14, 15, 16, 17, 18, 19, 20],
* [21, 22, 23, 24, 25, 26, 27],
* [28, 29, 30, 0, 0, 0, 0],
* ]
*/
function getCalendarTable($first, $days)
{
$calender = array();
for($i = 0; $i < $first; $i++) {
$calender[0][] = 0;
}
for($i = 0; $i < $days; $i++) {
for($j = 0; $j < 7; $j++) {
$calender[$j][] = $i + 1;
if(count($calender) == 7) {
continue;
}
}
}
if(count($calender[$j]) != 7) {
for($i = 0;$i < 7 - count($calender[$j]);$i++){
$calender[$j][] = 0;
}
}
return $calender;
}
assert(
getCalendarTable(0, 28) === [
[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
],
'1日が日曜日で28日間ある月'
);
assert(
getCalendarTable(0, 31) === [
[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 0, 0, 0, 0],
],
'1日が日曜日で31日間ある月'
);
assert(
getCalendarTable(1, 30) === [
[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 0, 0, 0, 0],
],
'1日が月曜日で30日間ある月'
);
assert(
getCalendarTable(4, 30) === [
[ 0, 0, 0, 0, 1, 2, 3],
[ 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30, 0],
],
'1日が木曜日で30日間ある月'
);
assert(
getCalendarTable(4, 31) === [
[ 0, 0, 0, 0, 1, 2, 3],
[ 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30, 31],
],
'1日が木曜日で31日間ある月'
);
assert(
getCalendarTable(5, 31) === [
[ 0, 0, 0, 0, 0, 1, 2],
[ 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30],
[31, 0, 0, 0, 0, 0, 0],
],
'1日が金曜日で31日ある月'
);