1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Contains the Calendar_Engine_UnixTS 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 Harry Fuecks <[email protected]> |
34
|
|
|
* @copyright 2003-2007 Harry Fuecks |
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
|
|
|
* Performs calendar calculations based on the PHP date() function and |
42
|
|
|
* Unix timestamps (using PHP's mktime() function). |
43
|
|
|
* |
44
|
|
|
* @category Date and Time |
45
|
|
|
* |
46
|
|
|
* @author Harry Fuecks <[email protected]> |
47
|
|
|
* @copyright 2003-2007 Harry Fuecks |
48
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
49
|
|
|
* |
50
|
|
|
* @link http://pear.php.net/package/Calendar |
51
|
|
|
*/ |
52
|
|
|
class Calendar_Engine_UnixTS /* implements Calendar_Engine_Interface */ |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* Makes sure a given timestamp is only ever parsed once |
56
|
|
|
* <pre> |
57
|
|
|
* array ( |
58
|
|
|
* [0] => year (e.g 2003), |
59
|
|
|
* [1] => month (e.g 9), |
60
|
|
|
* [2] => day (e.g 6), |
61
|
|
|
* [3] => hour (e.g 14), |
62
|
|
|
* [4] => minute (e.g 34), |
63
|
|
|
* [5] => second (e.g 45), |
64
|
|
|
* [6] => num days in month (e.g. 31), |
65
|
|
|
* [7] => week in year (e.g. 50), |
66
|
|
|
* [8] => day in week (e.g. 0 for Sunday) |
67
|
|
|
* ) |
68
|
|
|
* </pre> |
69
|
|
|
* Uses a static variable to prevent date() being used twice |
70
|
|
|
* for a date which is already known. |
71
|
|
|
* |
72
|
|
|
* @param int $stamp Unix timestamp |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function stampCollection($stamp) |
77
|
|
|
{ |
78
|
|
|
static $stamps = []; |
79
|
|
|
if (!isset($stamps[$stamp])) { |
80
|
|
|
$date = @date('Y n j H i s t W w', $stamp); |
81
|
|
|
$stamps[$stamp] = sscanf($date, '%d %d %d %d %d %d %d %d %d'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $stamps[$stamp]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns a numeric year given a timestamp. |
89
|
|
|
* |
90
|
|
|
* @param int $stamp Unix timestamp |
91
|
|
|
* |
92
|
|
|
* @return int year (e.g. 2003) |
93
|
|
|
*/ |
94
|
|
|
public function stampToYear($stamp) |
95
|
|
|
{ |
96
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
97
|
|
|
|
98
|
|
|
return (int)$date[0]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns a numeric month given a timestamp. |
103
|
|
|
* |
104
|
|
|
* @param int $stamp Unix timestamp |
105
|
|
|
* |
106
|
|
|
* @return int month (e.g. 9) |
107
|
|
|
*/ |
108
|
|
|
public function stampToMonth($stamp) |
109
|
|
|
{ |
110
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return (int)$date[1]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns a numeric day given a timestamp. |
117
|
|
|
* |
118
|
|
|
* @param int $stamp Unix timestamp |
119
|
|
|
* |
120
|
|
|
* @return int day (e.g. 15) |
121
|
|
|
*/ |
122
|
|
|
public function stampToDay($stamp) |
123
|
|
|
{ |
124
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
125
|
|
|
|
126
|
|
|
return (int)$date[2]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns a numeric hour given a timestamp. |
131
|
|
|
* |
132
|
|
|
* @param int $stamp Unix timestamp |
133
|
|
|
* |
134
|
|
|
* @return int hour (e.g. 13) |
135
|
|
|
*/ |
136
|
|
|
public function stampToHour($stamp) |
137
|
|
|
{ |
138
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
139
|
|
|
|
140
|
|
|
return (int)$date[3]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns a numeric minute given a timestamp. |
145
|
|
|
* |
146
|
|
|
* @param int $stamp Unix timestamp |
147
|
|
|
* |
148
|
|
|
* @return int minute (e.g. 34) |
149
|
|
|
*/ |
150
|
|
|
public function stampToMinute($stamp) |
151
|
|
|
{ |
152
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
153
|
|
|
|
154
|
|
|
return (int)$date[4]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns a numeric second given a timestamp. |
159
|
|
|
* |
160
|
|
|
* @param int $stamp Unix timestamp |
161
|
|
|
* |
162
|
|
|
* @return int second (e.g. 51) |
163
|
|
|
*/ |
164
|
|
|
public function stampToSecond($stamp) |
165
|
|
|
{ |
166
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
167
|
|
|
|
168
|
|
|
return (int)$date[5]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns a timestamp. |
173
|
|
|
* |
174
|
|
|
* @param int $y year (2003) |
175
|
|
|
* @param int $m month (9) |
176
|
|
|
* @param int $d day (13) |
177
|
|
|
* @param int $h hour (13) |
178
|
|
|
* @param int $i minute (34) |
179
|
|
|
* @param int $s second (53) |
180
|
|
|
* |
181
|
|
|
* @return int Unix timestamp |
182
|
|
|
*/ |
183
|
|
|
public function dateToStamp($y, $m, $d, $h = 0, $i = 0, $s = 0) |
184
|
|
|
{ |
185
|
|
|
static $dates = []; |
186
|
|
|
if (!isset($dates[$y][$m][$d][$h][$i][$s])) { |
187
|
|
|
$dates[$y][$m][$d][$h][$i][$s] = @mktime($h, $i, $s, $m, $d, $y); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $dates[$y][$m][$d][$h][$i][$s]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* The upper limit on years that the Calendar Engine can work with. |
195
|
|
|
* |
196
|
|
|
* @return int (2037) |
197
|
|
|
*/ |
198
|
|
|
public function getMaxYears() |
199
|
|
|
{ |
200
|
|
|
return 2037; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* The lower limit on years that the Calendar Engine can work with. |
205
|
|
|
* |
206
|
|
|
* @return int (1970 if it's Windows and 1902 for all other OSs) |
207
|
|
|
*/ |
208
|
|
|
public function getMinYears() |
209
|
|
|
{ |
210
|
|
|
return $min = false === mb_strpos(PHP_OS, 'WIN') ? 1902 : 1970; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the number of months in a year. |
215
|
|
|
* |
216
|
|
|
* @param int $y year |
217
|
|
|
* |
218
|
|
|
* @return int (12) |
219
|
|
|
*/ |
220
|
|
|
public function getMonthsInYear($y = null) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
return 12; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns the number of days in a month, given year and month. |
227
|
|
|
* |
228
|
|
|
* @param int $y year (2003) |
229
|
|
|
* @param int $m month (9) |
230
|
|
|
* |
231
|
|
|
* @return int days in month |
232
|
|
|
*/ |
233
|
|
|
public function getDaysInMonth($y, $m) |
234
|
|
|
{ |
235
|
|
|
$stamp = self::dateToStamp($y, $m, 1); |
|
|
|
|
236
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
237
|
|
|
|
238
|
|
|
return $date[6]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns numeric representation of the day of the week in a month, |
243
|
|
|
* given year and month. |
244
|
|
|
* |
245
|
|
|
* @param int $y year (2003) |
246
|
|
|
* @param int $m month (9) |
247
|
|
|
* |
248
|
|
|
* @return int from 0 to 6 |
249
|
|
|
*/ |
250
|
|
|
public function getFirstDayInMonth($y, $m) |
251
|
|
|
{ |
252
|
|
|
$stamp = self::dateToStamp($y, $m, 1); |
|
|
|
|
253
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
254
|
|
|
|
255
|
|
|
return $date[8]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns the number of days in a week. |
260
|
|
|
* |
261
|
|
|
* @param int $y year (2003) |
262
|
|
|
* @param int $m month (9) |
263
|
|
|
* @param int $d day (4) |
264
|
|
|
* |
265
|
|
|
* @return int (7) |
266
|
|
|
*/ |
267
|
|
|
public function getDaysInWeek($y = null, $m = null, $d = null) |
|
|
|
|
268
|
|
|
{ |
269
|
|
|
return 7; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Returns the number of the week in the year (ISO-8601), given a date. |
274
|
|
|
* |
275
|
|
|
* @param int $y year (2003) |
276
|
|
|
* @param int $m month (9) |
277
|
|
|
* @param int $d day (4) |
278
|
|
|
* |
279
|
|
|
* @return int week number |
280
|
|
|
*/ |
281
|
|
|
public function getWeekNInYear($y, $m, $d) |
282
|
|
|
{ |
283
|
|
|
$stamp = self::dateToStamp($y, $m, $d); |
|
|
|
|
284
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
285
|
|
|
|
286
|
|
|
return $date[7]; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns the number of the week in the month, given a date. |
291
|
|
|
* |
292
|
|
|
* @param int $y year (2003) |
293
|
|
|
* @param int $m month (9) |
294
|
|
|
* @param int $d day (4) |
295
|
|
|
* @param int $firstDay first day of the week (default: monday) |
296
|
|
|
* |
297
|
|
|
* @return int week number |
298
|
|
|
*/ |
299
|
|
|
public function getWeekNInMonth($y, $m, $d, $firstDay = 1) |
300
|
|
|
{ |
301
|
|
|
$weekEnd = (0 == $firstDay) ? $this->getDaysInWeek() - 1 : $firstDay - 1; |
302
|
|
|
$end_of_week = 1; |
303
|
|
|
while (@date('w', @mktime(0, 0, 0, $m, $end_of_week, $y)) != $weekEnd) { |
|
|
|
|
304
|
|
|
++$end_of_week; //find first weekend of the month |
305
|
|
|
} |
306
|
|
|
$w = 1; |
307
|
|
|
while ($d > $end_of_week) { |
308
|
|
|
++$w; |
309
|
|
|
$end_of_week += $this->getDaysInWeek(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $w; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Returns the number of weeks in the month. |
317
|
|
|
* |
318
|
|
|
* @param int $y year (2003) |
319
|
|
|
* @param int $m month (9) |
320
|
|
|
* @param int $firstDay first day of the week (default: monday) |
321
|
|
|
* |
322
|
|
|
* @return int weeks number |
323
|
|
|
*/ |
324
|
|
|
public function getWeeksInMonth($y, $m, $firstDay = 1) |
325
|
|
|
{ |
326
|
|
|
$FDOM = $this->getFirstDayInMonth($y, $m); |
327
|
|
|
if (0 == $FDOM) { |
328
|
|
|
$FDOM = $this->getDaysInWeek(); |
329
|
|
|
} |
330
|
|
|
if ($FDOM > $firstDay) { |
331
|
|
|
$daysInTheFirstWeek = $this->getDaysInWeek() - $FDOM + $firstDay; |
332
|
|
|
$weeks = 1; |
333
|
|
|
} else { |
334
|
|
|
$daysInTheFirstWeek = $firstDay - $FDOM; |
335
|
|
|
$weeks = 0; |
336
|
|
|
} |
337
|
|
|
$daysInTheFirstWeek %= $this->getDaysInWeek(); |
338
|
|
|
|
339
|
|
|
return (int)(ceil(($this->getDaysInMonth($y, $m) - $daysInTheFirstWeek) / $this->getDaysInWeek()) + $weeks); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns the number of the day of the week (0=sunday, 1=monday...). |
344
|
|
|
* |
345
|
|
|
* @param int $y year (2003) |
346
|
|
|
* @param int $m month (9) |
347
|
|
|
* @param int $d day (4) |
348
|
|
|
* |
349
|
|
|
* @return int weekday number |
350
|
|
|
*/ |
351
|
|
|
public function getDayOfWeek($y, $m, $d) |
352
|
|
|
{ |
353
|
|
|
$stamp = self::dateToStamp($y, $m, $d); |
|
|
|
|
354
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
355
|
|
|
|
356
|
|
|
return $date[8]; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Returns a list of integer days of the week beginning 0. |
361
|
|
|
* |
362
|
|
|
* @param int $y year (2003) |
363
|
|
|
* @param int $m month (9) |
364
|
|
|
* @param int $d day (4) |
365
|
|
|
* |
366
|
|
|
* @return array (0,1,2,3,4,5,6) 1 = Monday |
367
|
|
|
*/ |
368
|
|
|
public function getWeekDays($y = null, $m = null, $d = null) |
|
|
|
|
369
|
|
|
{ |
370
|
|
|
return [0, 1, 2, 3, 4, 5, 6]; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Returns the default first day of the week. |
375
|
|
|
* |
376
|
|
|
* @param int $y year (2003) |
377
|
|
|
* @param int $m month (9) |
378
|
|
|
* @param int $d day (4) |
379
|
|
|
* |
380
|
|
|
* @return int (default 1 = Monday) |
381
|
|
|
*/ |
382
|
|
|
public function getFirstDayOfWeek($y = null, $m = null, $d = null) |
|
|
|
|
383
|
|
|
{ |
384
|
|
|
return 1; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Returns the number of hours in a day. |
389
|
|
|
* |
390
|
|
|
* @param int $y year (2003) |
391
|
|
|
* @param int $m month (9) |
392
|
|
|
* @param int $d day (4) |
393
|
|
|
* |
394
|
|
|
* @return int (24) |
395
|
|
|
*/ |
396
|
|
|
public function getHoursInDay($y = null, $m = null, $d = null) |
|
|
|
|
397
|
|
|
{ |
398
|
|
|
return 24; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Returns the number of minutes in an hour. |
403
|
|
|
* |
404
|
|
|
* @param int $y year (2003) |
405
|
|
|
* @param int $m month (9) |
406
|
|
|
* @param int $d day (4) |
407
|
|
|
* @param int $h hour |
408
|
|
|
* |
409
|
|
|
* @return int (_EXTCAL_TS_MINUTE) |
410
|
|
|
*/ |
411
|
|
|
public function getMinutesInHour($y = null, $m = null, $d = null, $h = null) |
|
|
|
|
412
|
|
|
{ |
413
|
|
|
return _EXTCAL_TS_MINUTE; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Returns the number of seconds in a minutes. |
418
|
|
|
* |
419
|
|
|
* @param int $y year (2003) |
420
|
|
|
* @param int $m month (9) |
421
|
|
|
* @param int $d day (4) |
422
|
|
|
* @param int $h hour |
423
|
|
|
* @param int $i minute |
424
|
|
|
* |
425
|
|
|
* @return int (_EXTCAL_TS_MINUTE) |
426
|
|
|
*/ |
427
|
|
|
public function getSecondsInMinute($y = null, $m = null, $d = null, $h = null, $i = null) |
|
|
|
|
428
|
|
|
{ |
429
|
|
|
return _EXTCAL_TS_MINUTE; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Checks if the given day is the current day. |
434
|
|
|
* |
435
|
|
|
* @param mixed $stamp Any timestamp format recognized by Pear::Date |
436
|
|
|
* |
437
|
|
|
* @return bool |
438
|
|
|
*/ |
439
|
|
|
public function isToday($stamp) |
440
|
|
|
{ |
441
|
|
|
static $today = null; |
442
|
|
|
if (null === $today) { |
443
|
|
|
$today_date = @date('Y n j'); |
444
|
|
|
$today = sscanf($today_date, '%d %d %d'); |
445
|
|
|
} |
446
|
|
|
$date = self::stampCollection($stamp); |
|
|
|
|
447
|
|
|
|
448
|
|
|
return $date[2] == $today[2] && $date[1] == $today[1] && $date[0] == $today[0]; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|