1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Contains the Calendar_Engine_PearDate class. |
7
|
|
|
* |
8
|
|
|
* PHP versions 4 and 5 |
9
|
|
|
* |
10
|
|
|
* LICENSE: Redistribution and use in source and binary forms, with or without |
11
|
|
|
* modification, are permitted provided that the following conditions are met: |
12
|
|
|
* 1. Redistributions of source code must retain the above copyright |
13
|
|
|
* notice, this list of conditions and the following disclaimer. |
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in the |
16
|
|
|
* documentation and/or other materials provided with the distribution. |
17
|
|
|
* 3. The name of the author may not be used to endorse or promote products |
18
|
|
|
* derived from this software without specific prior written permission. |
19
|
|
|
* |
20
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED |
21
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
22
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
23
|
|
|
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY |
24
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
25
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
26
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
27
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
29
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30
|
|
|
* |
31
|
|
|
* @category Date and Time |
32
|
|
|
* |
33
|
|
|
* @author Lorenzo Alberton <[email protected]> |
34
|
|
|
* @copyright 2003-2007 Lorenzo Alberton |
35
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
36
|
|
|
* |
37
|
|
|
* @link http://pear.php.net/package/Calendar |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Load PEAR::Date class. |
42
|
|
|
*/ |
43
|
|
|
require_once __DIR__ . '/Date.php'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Performs calendar calculations based on the PEAR::Date class |
47
|
|
|
* Timestamps are in the ISO-8601 format (YYYY-MM-DD HH:MM:SS). |
48
|
|
|
* |
49
|
|
|
* @category Date and Time |
50
|
|
|
* |
51
|
|
|
* @author Lorenzo Alberton <[email protected]> |
52
|
|
|
* @copyright 2003-2007 Lorenzo Alberton |
53
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
54
|
|
|
* |
55
|
|
|
* @link http://pear.php.net/package/Calendar |
56
|
|
|
*/ |
57
|
|
|
class Calendar_Engine_PearDate /* implements Calendar_Engine_Interface */ |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* Makes sure a given timestamp is only ever parsed once |
61
|
|
|
* Uses a static variable to prevent date() being used twice |
62
|
|
|
* for a date which is already known. |
63
|
|
|
* |
64
|
|
|
* @param mixed $stamp Any timestamp format recognized by Pear::Date |
65
|
|
|
* |
66
|
|
|
* @return object Pear::Date object |
67
|
|
|
*/ |
68
|
|
|
public function stampCollection($stamp) |
69
|
|
|
{ |
70
|
|
|
static $stamps = []; |
71
|
|
|
if (!isset($stamps[$stamp])) { |
72
|
|
|
$stamps[$stamp] = new Date($stamp); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $stamps[$stamp]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a numeric year given a iso-8601 datetime. |
80
|
|
|
* |
81
|
|
|
* @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
82
|
|
|
* |
83
|
|
|
* @return int year (e.g. 2003) |
84
|
|
|
*/ |
85
|
|
|
public function stampToYear($stamp) |
86
|
|
|
{ |
87
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
88
|
|
|
|
89
|
|
|
return (int)$date->year; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns a numeric month given a iso-8601 datetime. |
94
|
|
|
* |
95
|
|
|
* @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
96
|
|
|
* |
97
|
|
|
* @return int month (e.g. 9) |
98
|
|
|
*/ |
99
|
|
|
public function stampToMonth($stamp) |
100
|
|
|
{ |
101
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return (int)$date->month; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a numeric day given a iso-8601 datetime. |
108
|
|
|
* |
109
|
|
|
* @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
110
|
|
|
* |
111
|
|
|
* @return int day (e.g. 15) |
112
|
|
|
*/ |
113
|
|
|
public function stampToDay($stamp) |
114
|
|
|
{ |
115
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
116
|
|
|
|
117
|
|
|
return (int)$date->day; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a numeric hour given a iso-8601 datetime. |
122
|
|
|
* |
123
|
|
|
* @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
124
|
|
|
* |
125
|
|
|
* @return int hour (e.g. 13) |
126
|
|
|
*/ |
127
|
|
|
public function stampToHour($stamp) |
128
|
|
|
{ |
129
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
130
|
|
|
|
131
|
|
|
return (int)$date->hour; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns a numeric minute given a iso-8601 datetime. |
136
|
|
|
* |
137
|
|
|
* @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
138
|
|
|
* |
139
|
|
|
* @return int minute (e.g. 34) |
140
|
|
|
*/ |
141
|
|
|
public function stampToMinute($stamp) |
142
|
|
|
{ |
143
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
144
|
|
|
|
145
|
|
|
return (int)$date->minute; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns a numeric second given a iso-8601 datetime. |
150
|
|
|
* |
151
|
|
|
* @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
152
|
|
|
* |
153
|
|
|
* @return int second (e.g. 51) |
154
|
|
|
*/ |
155
|
|
|
public function stampToSecond($stamp) |
156
|
|
|
{ |
157
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
158
|
|
|
|
159
|
|
|
return (int)$date->second; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns a iso-8601 datetime. |
164
|
|
|
* |
165
|
|
|
* @param int $y year (2003) |
166
|
|
|
* @param int $m month (9) |
167
|
|
|
* @param int $d day (13) |
168
|
|
|
* @param int $h hour (13) |
169
|
|
|
* @param int $i minute (34) |
170
|
|
|
* @param int $s second (53) |
171
|
|
|
* |
172
|
|
|
* @return string iso-8601 datetime |
173
|
|
|
*/ |
174
|
|
|
public function dateToStamp($y, $m, $d, $h = 0, $i = 0, $s = 0) |
175
|
|
|
{ |
176
|
|
|
$r = []; |
177
|
|
|
self::adjustDate($y, $m, $d, $h, $i, $s); |
|
|
|
|
178
|
|
|
$key = $y . $m . $d . $h . $i . $s; |
179
|
|
|
if (!isset($r[$key])) { |
180
|
|
|
$r[$key] = sprintf('%04d-%02d-%02d %02d:%02d:%02d', $y, $m, $d, $h, $i, $s); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $r[$key]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the correct date values (useful for math operations on dates). |
188
|
|
|
* |
189
|
|
|
* @param int &$y year (2003) |
190
|
|
|
* @param int &$m month (9) |
191
|
|
|
* @param int &$d day (13) |
192
|
|
|
* @param int &$h hour (13) |
193
|
|
|
* @param int &$i minute (34) |
194
|
|
|
* @param int &$s second (53) |
195
|
|
|
*/ |
196
|
|
|
public function adjustDate(&$y, &$m, &$d, &$h, &$i, &$s) |
197
|
|
|
{ |
198
|
|
|
if ($s < 0) { |
199
|
|
|
$m -= floor($s / _EXTCAL_TS_MINUTE); |
200
|
|
|
$s = -$s % _EXTCAL_TS_MINUTE; |
201
|
|
|
} |
202
|
|
|
if ($s > _EXTCAL_TS_MINUTE) { |
203
|
|
|
$m += floor($s / _EXTCAL_TS_MINUTE); |
204
|
|
|
$s %= _EXTCAL_TS_MINUTE; |
205
|
|
|
} |
206
|
|
|
if ($i < 0) { |
207
|
|
|
$h -= floor($i / _EXTCAL_TS_MINUTE); |
208
|
|
|
$i = -$i % _EXTCAL_TS_MINUTE; |
209
|
|
|
} |
210
|
|
|
if ($i > _EXTCAL_TS_MINUTE) { |
211
|
|
|
$h += floor($i / _EXTCAL_TS_MINUTE); |
212
|
|
|
$i %= _EXTCAL_TS_MINUTE; |
213
|
|
|
} |
214
|
|
|
if ($h < 0) { |
215
|
|
|
$d -= floor($h / 24); |
216
|
|
|
$h = -$h % 24; |
217
|
|
|
} |
218
|
|
|
if ($h > 24) { |
219
|
|
|
$d += floor($h / 24); |
220
|
|
|
$h %= 24; |
221
|
|
|
} |
222
|
|
|
for (; $m < 1; $y--, $m += 12) { |
223
|
|
|
} |
224
|
|
|
for (; $m > 12; $y++, $m -= 12) { |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
while ($d < 1) { |
228
|
|
|
if ($m > 1) { |
229
|
|
|
--$m; |
230
|
|
|
} else { |
231
|
|
|
$m = 12; |
232
|
|
|
--$y; |
233
|
|
|
} |
234
|
|
|
$d += Date_Calc::daysInMonth($m, $y); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
for ($max_days = Date_Calc::daysInMonth($m, $y); $d > $max_days;) { |
237
|
|
|
$d -= $max_days; |
238
|
|
|
if ($m < 12) { |
239
|
|
|
++$m; |
240
|
|
|
} else { |
241
|
|
|
$m = 1; |
242
|
|
|
++$y; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* The upper limit on years that the Calendar Engine can work with. |
249
|
|
|
* |
250
|
|
|
* @return int 9999 |
251
|
|
|
*/ |
252
|
|
|
public function getMaxYears() |
253
|
|
|
{ |
254
|
|
|
return 9999; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* The lower limit on years that the Calendar Engine can work with. |
259
|
|
|
* |
260
|
|
|
* @return int |
261
|
|
|
*/ |
262
|
|
|
public function getMinYears() |
263
|
|
|
{ |
264
|
|
|
return 0; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Returns the number of months in a year. |
269
|
|
|
* |
270
|
|
|
* @param int $y year |
271
|
|
|
* |
272
|
|
|
* @return int (12) |
273
|
|
|
*/ |
274
|
|
|
public function getMonthsInYear($y = null) |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
return 12; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Returns the number of days in a month, given year and month. |
281
|
|
|
* |
282
|
|
|
* @param int $y year (2003) |
283
|
|
|
* @param int $m month (9) |
284
|
|
|
* |
285
|
|
|
* @return int days in month |
286
|
|
|
*/ |
287
|
|
|
public function getDaysInMonth($y, $m) |
288
|
|
|
{ |
289
|
|
|
return (int)Date_Calc::daysInMonth($m, $y); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns numeric representation of the day of the week in a month, |
294
|
|
|
* given year and month. |
295
|
|
|
* |
296
|
|
|
* @param int $y year (2003) |
297
|
|
|
* @param int $m month (9) |
298
|
|
|
* |
299
|
|
|
* @return int from 0 to 7 |
300
|
|
|
*/ |
301
|
|
|
public function getFirstDayInMonth($y, $m) |
302
|
|
|
{ |
303
|
|
|
return (int)Date_Calc::dayOfWeek(1, $m, $y); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns the number of days in a week. |
308
|
|
|
* |
309
|
|
|
* @param int $y year (2003) |
310
|
|
|
* @param int $m month (9) |
311
|
|
|
* @param int $d day (4) |
312
|
|
|
* |
313
|
|
|
* @return int (7) |
314
|
|
|
*/ |
315
|
|
|
public function getDaysInWeek($y = null, $m = null, $d = null) |
|
|
|
|
316
|
|
|
{ |
317
|
|
|
return 7; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Returns the number of the week in the year (ISO-8601), given a date. |
322
|
|
|
* |
323
|
|
|
* @param int $y year (2003) |
324
|
|
|
* @param int $m month (9) |
325
|
|
|
* @param int $d day (4) |
326
|
|
|
* |
327
|
|
|
* @return int week number |
328
|
|
|
*/ |
329
|
|
|
public function getWeekNInYear($y, $m, $d) |
330
|
|
|
{ |
331
|
|
|
//return Date_Calc::weekOfYear($d, $m, $y); //beware, Date_Calc doesn't follow ISO-8601 standard! |
332
|
|
|
list($nYear, $nWeek) = Date_Calc::weekOfYear4th($d, $m, $y); |
333
|
|
|
|
334
|
|
|
return $nWeek; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Returns the number of the week in the month, given a date. |
339
|
|
|
* |
340
|
|
|
* @param int $y year (2003) |
341
|
|
|
* @param int $m month (9) |
342
|
|
|
* @param int $d day (4) |
343
|
|
|
* @param int $firstDay first day of the week (default: monday) |
344
|
|
|
* |
345
|
|
|
* @return int week number |
346
|
|
|
*/ |
347
|
|
|
public function getWeekNInMonth($y, $m, $d, $firstDay = 1) |
348
|
|
|
{ |
349
|
|
|
$weekEnd = (0 == $firstDay) ? $this->getDaysInWeek() - 1 : $firstDay - 1; |
350
|
|
|
$end_of_week = (int)Date_Calc::nextDayOfWeek($weekEnd, 1, $m, $y, '%e', true); |
351
|
|
|
$w = 1; |
352
|
|
|
while ($d > $end_of_week) { |
353
|
|
|
++$w; |
354
|
|
|
$end_of_week += $this->getDaysInWeek(); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return $w; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Returns the number of weeks in the month. |
362
|
|
|
* |
363
|
|
|
* @param int $y year (2003) |
364
|
|
|
* @param int $m month (9) |
365
|
|
|
* @param int $firstDay first day of the week (default: monday) |
366
|
|
|
* |
367
|
|
|
* @return int weeks number |
368
|
|
|
*/ |
369
|
|
|
public function getWeeksInMonth($y, $m, $firstDay = 1) |
370
|
|
|
{ |
371
|
|
|
$FDOM = Date_Calc::firstOfMonthWeekday($m, $y); |
372
|
|
|
if (0 == $FDOM) { |
373
|
|
|
$FDOM = $this->getDaysInWeek(); |
374
|
|
|
} |
375
|
|
|
if ($FDOM > $firstDay) { |
376
|
|
|
$daysInTheFirstWeek = $this->getDaysInWeek() - $FDOM + $firstDay; |
377
|
|
|
$weeks = 1; |
378
|
|
|
} else { |
379
|
|
|
$daysInTheFirstWeek = $firstDay - $FDOM; |
380
|
|
|
$weeks = 0; |
381
|
|
|
} |
382
|
|
|
$daysInTheFirstWeek %= $this->getDaysInWeek(); |
383
|
|
|
|
384
|
|
|
return (int)(ceil(($this->getDaysInMonth($y, $m) - $daysInTheFirstWeek) / $this->getDaysInWeek()) + $weeks); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Returns the number of the day of the week (0=sunday, 1=monday...). |
389
|
|
|
* |
390
|
|
|
* @param int $y year (2003) |
391
|
|
|
* @param int $m month (9) |
392
|
|
|
* @param int $d day (4) |
393
|
|
|
* |
394
|
|
|
* @return int weekday number |
395
|
|
|
*/ |
396
|
|
|
public function getDayOfWeek($y, $m, $d) |
397
|
|
|
{ |
398
|
|
|
return Date_Calc::dayOfWeek($d, $m, $y); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Returns a list of integer days of the week beginning 0. |
403
|
|
|
* |
404
|
|
|
* @param int $y year (2003) |
405
|
|
|
* @param int $m month (9) |
406
|
|
|
* @param int $d day (4) |
407
|
|
|
* |
408
|
|
|
* @return array (0, 1, 2, 3, 4, 5, 6) 1 = Monday |
409
|
|
|
*/ |
410
|
|
|
public function getWeekDays($y = null, $m = null, $d = null) |
|
|
|
|
411
|
|
|
{ |
412
|
|
|
return [0, 1, 2, 3, 4, 5, 6]; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Returns the default first day of the week. |
417
|
|
|
* |
418
|
|
|
* @param int $y year (2003) |
419
|
|
|
* @param int $m month (9) |
420
|
|
|
* @param int $d day (4) |
421
|
|
|
* |
422
|
|
|
* @return int (default 1 = Monday) |
423
|
|
|
*/ |
424
|
|
|
public function getFirstDayOfWeek($y = null, $m = null, $d = null) |
|
|
|
|
425
|
|
|
{ |
426
|
|
|
return 1; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Returns the number of hours in a day. |
431
|
|
|
* |
432
|
|
|
* @param int $y year (2003) |
433
|
|
|
* @param int $m month (9) |
434
|
|
|
* @param int $d day (4) |
435
|
|
|
* |
436
|
|
|
* @return int (24) |
437
|
|
|
*/ |
438
|
|
|
public function getHoursInDay($y = null, $m = null, $d = null) |
|
|
|
|
439
|
|
|
{ |
440
|
|
|
return 24; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Returns the number of minutes in an hour. |
445
|
|
|
* |
446
|
|
|
* @param int $y year (2003) |
447
|
|
|
* @param int $m month (9) |
448
|
|
|
* @param int $d day (4) |
449
|
|
|
* @param int $h hour |
450
|
|
|
* |
451
|
|
|
* @return int (_EXTCAL_TS_MINUTE) |
452
|
|
|
*/ |
453
|
|
|
public function getMinutesInHour($y = null, $m = null, $d = null, $h = null) |
|
|
|
|
454
|
|
|
{ |
455
|
|
|
return _EXTCAL_TS_MINUTE; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Returns the number of seconds in a minutes. |
460
|
|
|
* |
461
|
|
|
* @param int $y year (2003) |
462
|
|
|
* @param int $m month (9) |
463
|
|
|
* @param int $d day (4) |
464
|
|
|
* @param int $h hour |
465
|
|
|
* @param int $i minute |
466
|
|
|
* |
467
|
|
|
* @return int (_EXTCAL_TS_MINUTE) |
468
|
|
|
*/ |
469
|
|
|
public function getSecondsInMinute($y = null, $m = null, $d = null, $h = null, $i = null) |
|
|
|
|
470
|
|
|
{ |
471
|
|
|
return _EXTCAL_TS_MINUTE; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Checks if the given day is the current day. |
476
|
|
|
* |
477
|
|
|
* @param mixed $stamp Any timestamp format recognized by Pear::Date |
478
|
|
|
* |
479
|
|
|
* @return bool |
480
|
|
|
*/ |
481
|
|
|
public function isToday($stamp) |
482
|
|
|
{ |
483
|
|
|
static $today = null; |
484
|
|
|
if (null === $today) { |
485
|
|
|
$today = new Date(); |
486
|
|
|
} |
487
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
488
|
|
|
|
489
|
|
|
return $date->day == $today->getDay() && $date->month == $today->getMonth() |
490
|
|
|
&& $date->year == $today->getYear(); |
491
|
|
|
} |
492
|
|
|
} |
493
|
|
|
|