/
calendar.class.php
267 lines (242 loc) · 7.47 KB
/
calendar.class.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
define('LANG_CAL_WEEK', 'week');
/**
* A Generic Calender Class
* It Generates an Array of weeks and days as well as other properties of the calendar given month and year
*
* An Example HTML generation is included in function Calendar->getHTML()
* To get custom HTML, use the Calendar->days property. It is an array of weeks that contains arrays of days
*
* @copyright Creative-Commons 2.0
* @author gabe@fijiwebdesign.com - Please send me your suggestions
* @version $Id$
*
* Feel free to use as you wish, if you do show attribution please link to http://www.fijiwebdesign.com/
*
* Parts based on the following works and subject to their terms:
* http://scripts.franciscocharrua.com/php-calendar.php
* http://keithdevens.com/software/php_calendar/
*
*/
class Calendar {
/**
* Construct
*
* @param $str_date Any String formatted date understood by strtotime()
* @link http://www.php.net/manual/en/function.strtotime.php
*/
function Calendar($str_date = 'now') {
$this->time = strtotime($str_date);
$this->date = getDate($this->time);
// use current month and year if not supplied
$month = $this->date['mon'];
$year = $this->date['year'];
// get the dates for the start of this month and next month
// we don't worry about month=13 as mktime() will take care of this
$this_month = getDate(mktime(0, 0, 0, $month, 1, $year));
$next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year));
// http://scripts.franciscocharrua.com/php-calendar.php
$this->day = $this_month["mday"];
$this->month = $this_month["mon"];
$this->month_name = $this_month["month"];
$this->year = $this_month["year"];
$this->first_day_offset = $this_month["wday"];
$this->days_count = round(($next_month[0] - $this_month[0]) / (60 * 60 * 24));
$this->days = array();
// fill in days
$week = 1;
for($i = 0; $i < $this->days_count+$this->first_day_offset; $i++) {
if ($i < $this->first_day_offset) {
$this->days[$week][] = null;
} else {
$this->days[$week][] = $i-$this->first_day_offset+1;
}
if ($i%7 == 6) {
$week++;
}
}
// localized day names
$this->day_names = $this->_getFullDayNames();
}
/**
* Return the week number a day lies in
* @return Int
*/
function getWeekNumberByDay($day) {
$i = $this->first_day_offset+$day;
return ceil($i/7);
}
/**
* Return the week number that today lies in
* @return Int
*/
function getCurrentWeekNumber() {
$day = $this->date['mday'];
return $this->getWeekNumberByDay($day);
}
/**
* Return the Array representing the calendar days
* @return Array
*/
function getCalenderMonthDays() {
return $this->days;
}
/**
* Return the first day of the month
* @return Int
*/
function getCalenderMonthStart() {
return $this->days[0];
}
/**
* Return the last day of the month
* @return Int
*/
function getCalenderMonthEnd($week) {
return $this->days[count($this->days)-1];
}
/**
* Return the Array representing the days in the given week
* @param Int Week number
* @return Array
*/
function getCalenderWeekDays($week) {
if (isset($this->days[$week])) {
return $this->days[$week];
}
return false;
}
/**
* Return the first day of the given week
* @param Int Week number
* @return Int
*/
function getCalenderWeekStart($week) {
if (isset($this->days[$week])) {
for($i = 0; $i < 7; $i++) {
if ($this->days[$week][$i]) {
return $this->days[$week][$i];
}
}
}
return false;
}
/**
* Return the last day of the given week
* @param Int Week number
* @return Int
*/
function getCalenderWeekEnd($week) {
if (isset($this->days[$week])) {
for($i = 6; $i >= 0; $i--) {
if ($this->days[$week][$i]) {
return $this->days[$week][$i];
}
}
}
return false;
}
/**
* Generate a HTML display of the calendar month
* @return String HTML
* @param $header Bool[optional] Show the year and month header
* @param $day_names String[optional] Show the day names
* @param $day_name_lengh Int[optional] Length of day names
* @param $callback Function[optional] A callback function to call when each day is rendered to the calendar
*/
function getMonthHTML($header = true, $day_names = true, $day_name_lengh = 3, $callback = false) {
$days =& $this->getCalenderMonthDays();
$html[] = '<table class="calendar"><tbody>';
// print year and month
if ($header) {
$html[] = '<tr class="header">';
$html[] = '<th colspan="7" align="center">'.$this->month_name.' '.$this->year.'</th>';
$html[] = '</tr>';
}
// print the day names
if ($day_names) {
$html[] = '<tr class="day_names">';
for($i = 1; $i <= 7; $i++) {
$name = $this->getDayName($i, $day_name_lengh);
$html[] = '<th class="day_name '.$name.'">'.$name.'</th>';
}
$html[] = '</tr>';
}
// print the calendar days
foreach($days as $i=>$week) {
$html[] = '<tr class="week week'.$i.'">';
foreach($week as $j=>$day) {
if ($callback) {
$day = $callback($day, $i);
}
$html[] = '<td class="day '.$day_names[$j%7].'" rel="day'.$j.'">'.$day.'</td>';
}
$html[] = '</tr>';
}
$html[] = '<tbody></table>';
return implode("\n", $html);
}
/**
* Generate a HTML display of the calendar week
* @return String HTML
* @param $week Int The week number
* @param $header Bool[optional] Show the year and month header
* @param $day_names String[optional] Show the day names
* @param $day_name_lengh Int[optional] Length of day names
* @param $callback Function[optional] A callback function to call when each day is rendered
*/
function getWeekHTML($week, $header = true, $day_names = true, $day_name_lengh = 3, $callback = false) {
$days =& $this->getCalenderWeekDays($week);
$html[] = '<table class="calendar"><tbody>';
// print year and month
if ($header) {
$html[] = '<tr class="header">';
$html[] = '<th colspan="7" align="center">'.$this->month_name.' '.$this->year.': '.ucfirst(LANG_CAL_WEEK).' '.$week.'</th>';
$html[] = '</tr>';
}
// print the day names
if ($day_names) {
$html[] = '<tr class="day_names">';
for($i = 1; $i <= 7; $i++) {
$name = $this->getDayName($i, $day_name_lengh);
$html[] = '<th class="day_name '.$name.'">'.$name.'</th>';
}
$html[] = '</tr>';
}
// print the calendar days
$html[] = '<tr class="week week'.$week.'">';
foreach($days as $i=>$day) {
if ($callback) {
$day = $callback($day, $i);
}
$html[] = '<td class="day '.$day_names[$j%7].'" rel="day'.$j.'">'.$day.'</td>';
}
$html[] = '</tr>';
$html[] = '<tbody></table>';
return implode("\n", $html);
}
/**
* Returns localized full day names
* @private
* @author http://keithdevens.com/software/php_calendar/
* @return Array
*/
function _getFullDayNames() {
$day_names = array();
for($n=1,$t=3*86400; $n<=7; $n++,$t+=86400) {
$day_names[$n] = ucfirst(gmstrftime('%A',$t));
}
return $day_names;
}
/**
* Retrieve a Day name given the date
* @return String
* @param $date Int Day of month, eg: 3 or 31
*/
function getDayName($date, $len = null) {
$i = ($date+$this->first_day_offset)%7;
$i = $i ? $i : 7; // 7%7 = 0. Which should be 7 as we start count at 1
return $len ? substr($this->day_names[$i], 0, $len) : $this->day_names[$i];
}
}
?>