1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Contains the Calendar_Engine_UnixTS class |
6
|
|
|
* |
7
|
|
|
* PHP versions 4 and 5 |
8
|
|
|
* |
9
|
|
|
* LICENSE: Redistribution and use in source and binary forms, with or without |
10
|
|
|
* modification, are permitted provided that the following conditions are met: |
11
|
|
|
* 1. Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14
|
|
|
* notice, this list of conditions and the following disclaimer in the |
15
|
|
|
* documentation and/or other materials provided with the distribution. |
16
|
|
|
* 3. The name of the author may not be used to endorse or promote products |
17
|
|
|
* derived from this software without specific prior written permission. |
18
|
|
|
* |
19
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED |
20
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
21
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22
|
|
|
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY |
23
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29
|
|
|
* |
30
|
|
|
* @category Date and Time |
31
|
|
|
* @package Calendar |
32
|
|
|
* @author Harry Fuecks <[email protected]> |
33
|
|
|
* @copyright 2003-2007 Harry Fuecks |
34
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
35
|
|
|
* @version CVS: $Id$ |
36
|
|
|
* @link http://pear.php.net/package/Calendar |
37
|
|
|
*/ |
38
|
|
|
namespace PEAR\Calendar\Engine; |
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
|
|
|
* @package Calendar |
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
|
|
|
* @link http://pear.php.net/package/Calendar |
50
|
|
|
* @access protected |
51
|
|
|
*/ |
52
|
|
|
class 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
|
|
|
* @access protected |
76
|
|
|
*/ |
77
|
|
|
function stampCollection($stamp) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
static $stamps = array(); |
80
|
|
|
if ( !isset($stamps[$stamp]) ) { |
81
|
|
|
$date = @date('Y n j H i s t W w', $stamp); |
82
|
|
|
$stamps[$stamp] = sscanf($date, "%d %d %d %d %d %d %d %d %d"); |
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
|
|
|
* @access protected |
94
|
|
|
*/ |
95
|
|
|
function stampToYear($stamp) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
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
|
|
|
* @access protected |
108
|
|
|
*/ |
109
|
|
|
function stampToMonth($stamp) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
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
|
|
|
* @access protected |
122
|
|
|
*/ |
123
|
|
|
function stampToDay($stamp) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
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
|
|
|
* @access protected |
136
|
|
|
*/ |
137
|
|
|
function stampToHour($stamp) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
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
|
|
|
* @access protected |
150
|
|
|
*/ |
151
|
|
|
function stampToMinute($stamp) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
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
|
|
|
* @access protected |
164
|
|
|
*/ |
165
|
|
|
function stampToSecond($stamp) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
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
|
|
|
* @access protected |
183
|
|
|
*/ |
184
|
|
|
function dateToStamp($y, $m, $d, $h=0, $i=0, $s=0) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
static $dates = array(); |
187
|
|
|
if (!isset($dates[$y][$m][$d][$h][$i][$s])) { |
188
|
|
|
$dates[$y][$m][$d][$h][$i][$s] = @mktime($h, $i, $s, $m, $d, $y); |
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
|
|
|
* @access protected |
198
|
|
|
*/ |
199
|
|
|
function getMaxYears() |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
return 2037; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* The lower limit on years that the Calendar Engine can work with |
206
|
|
|
* |
207
|
|
|
* @return int (1970 if it's Windows and 1902 for all other OSs) |
208
|
|
|
* @access protected |
209
|
|
|
*/ |
210
|
|
|
function getMinYears() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
return $min = strpos(PHP_OS, 'WIN') === false ? 1902 : 1970; |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns the number of months in a year |
217
|
|
|
* |
218
|
|
|
* @param int $y year |
219
|
|
|
* |
220
|
|
|
* @return int (12) |
221
|
|
|
* @access protected |
222
|
|
|
*/ |
223
|
|
|
function getMonthsInYear($y=null) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
return 12; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the number of days in a month, given year and month |
230
|
|
|
* |
231
|
|
|
* @param int $y year (2003) |
232
|
|
|
* @param int $m month (9) |
233
|
|
|
* |
234
|
|
|
* @return int days in month |
235
|
|
|
* @access protected |
236
|
|
|
*/ |
237
|
|
|
function getDaysInMonth($y, $m) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$stamp = UnixTS::dateToStamp($y, $m, 1); |
|
|
|
|
240
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
241
|
|
|
return $date[6]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns numeric representation of the day of the week in a month, |
246
|
|
|
* given year and month |
247
|
|
|
* |
248
|
|
|
* @param int $y year (2003) |
249
|
|
|
* @param int $m month (9) |
250
|
|
|
* |
251
|
|
|
* @return int from 0 to 6 |
252
|
|
|
* @access protected |
253
|
|
|
*/ |
254
|
|
|
function getFirstDayInMonth($y, $m) |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
$stamp = UnixTS::dateToStamp($y, $m, 1); |
|
|
|
|
257
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
258
|
|
|
return $date[8]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the number of days in a week |
263
|
|
|
* |
264
|
|
|
* @param int $y year (2003) |
265
|
|
|
* @param int $m month (9) |
266
|
|
|
* @param int $d day (4) |
267
|
|
|
* |
268
|
|
|
* @return int (7) |
269
|
|
|
* @access protected |
270
|
|
|
*/ |
271
|
|
|
function getDaysInWeek($y=null, $m=null, $d=null) |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
return 7; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Returns the number of the week in the year (ISO-8601), given a date |
278
|
|
|
* |
279
|
|
|
* @param int $y year (2003) |
280
|
|
|
* @param int $m month (9) |
281
|
|
|
* @param int $d day (4) |
282
|
|
|
* |
283
|
|
|
* @return int week number |
284
|
|
|
* @access protected |
285
|
|
|
*/ |
286
|
|
|
function getWeekNInYear($y, $m, $d) |
|
|
|
|
287
|
|
|
{ |
288
|
|
|
$stamp = UnixTS::dateToStamp($y, $m, $d); |
|
|
|
|
289
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
290
|
|
|
return $date[7]; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Returns the number of the week in the month, given a date |
295
|
|
|
* |
296
|
|
|
* @param int $y year (2003) |
297
|
|
|
* @param int $m month (9) |
298
|
|
|
* @param int $d day (4) |
299
|
|
|
* @param int $firstDay first day of the week (default: monday) |
300
|
|
|
* |
301
|
|
|
* @return int week number |
302
|
|
|
* @access protected |
303
|
|
|
*/ |
304
|
|
|
function getWeekNInMonth($y, $m, $d, $firstDay=1) |
|
|
|
|
305
|
|
|
{ |
306
|
|
|
$weekEnd = (0 == $firstDay) ? $this->getDaysInWeek()-1 : $firstDay-1; |
307
|
|
|
$end_of_week = 1; |
308
|
|
|
while (@date('w', @mktime(0, 0, 0, $m, $end_of_week, $y)) != $weekEnd) { |
309
|
|
|
++$end_of_week; //find first weekend of the month |
310
|
|
|
} |
311
|
|
|
$w = 1; |
312
|
|
|
while ($d > $end_of_week) { |
313
|
|
|
++$w; |
314
|
|
|
$end_of_week += $this->getDaysInWeek(); |
315
|
|
|
} |
316
|
|
|
return $w; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Returns the number of weeks in the month |
321
|
|
|
* |
322
|
|
|
* @param int $y year (2003) |
323
|
|
|
* @param int $m month (9) |
324
|
|
|
* @param int $firstDay first day of the week (default: monday) |
325
|
|
|
* |
326
|
|
|
* @return int weeks number |
327
|
|
|
* @access protected |
328
|
|
|
*/ |
329
|
|
|
function getWeeksInMonth($y, $m, $firstDay = 1) |
|
|
|
|
330
|
|
|
{ |
331
|
|
|
$FDOM = $this->getFirstDayInMonth($y, $m); |
332
|
|
|
if ($FDOM == 0) { |
333
|
|
|
$FDOM = $this->getDaysInWeek(); |
334
|
|
|
} |
335
|
|
|
if ($FDOM > $firstDay) { |
336
|
|
|
$daysInTheFirstWeek = $this->getDaysInWeek() - $FDOM + $firstDay; |
337
|
|
|
$weeks = 1; |
338
|
|
|
} else { |
339
|
|
|
$daysInTheFirstWeek = $firstDay - $FDOM; |
340
|
|
|
$weeks = 0; |
341
|
|
|
} |
342
|
|
|
$daysInTheFirstWeek %= $this->getDaysInWeek(); |
343
|
|
|
return (int)(ceil(($this->getDaysInMonth($y, $m) - $daysInTheFirstWeek) / |
344
|
|
|
$this->getDaysInWeek()) + $weeks); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Returns the number of the day of the week (0=sunday, 1=monday...) |
349
|
|
|
* |
350
|
|
|
* @param int $y year (2003) |
351
|
|
|
* @param int $m month (9) |
352
|
|
|
* @param int $d day (4) |
353
|
|
|
* |
354
|
|
|
* @return int weekday number |
355
|
|
|
* @access protected |
356
|
|
|
*/ |
357
|
|
|
function getDayOfWeek($y, $m, $d) |
|
|
|
|
358
|
|
|
{ |
359
|
|
|
$stamp = UnixTS::dateToStamp($y, $m, $d); |
|
|
|
|
360
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
361
|
|
|
return $date[8]; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Returns a list of integer days of the week beginning 0 |
366
|
|
|
* |
367
|
|
|
* @param int $y year (2003) |
368
|
|
|
* @param int $m month (9) |
369
|
|
|
* @param int $d day (4) |
370
|
|
|
* |
371
|
|
|
* @return array (0,1,2,3,4,5,6) 1 = Monday |
372
|
|
|
* @access protected |
373
|
|
|
*/ |
374
|
|
|
function getWeekDays($y=null, $m=null, $d=null) |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
return array(0, 1, 2, 3, 4, 5, 6); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Returns the default first day of the week |
381
|
|
|
* |
382
|
|
|
* @param int $y year (2003) |
383
|
|
|
* @param int $m month (9) |
384
|
|
|
* @param int $d day (4) |
385
|
|
|
* |
386
|
|
|
* @return int (default 1 = Monday) |
387
|
|
|
* @access protected |
388
|
|
|
*/ |
389
|
|
|
function getFirstDayOfWeek($y=null, $m=null, $d=null) |
|
|
|
|
390
|
|
|
{ |
391
|
|
|
return 1; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Returns the number of hours in a day |
396
|
|
|
* |
397
|
|
|
* @param int $y year (2003) |
398
|
|
|
* @param int $m month (9) |
399
|
|
|
* @param int $d day (4) |
400
|
|
|
* |
401
|
|
|
* @return int (24) |
402
|
|
|
* @access protected |
403
|
|
|
*/ |
404
|
|
|
function getHoursInDay($y=null, $m=null, $d=null) |
|
|
|
|
405
|
|
|
{ |
406
|
|
|
return 24; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Returns the number of minutes in an hour |
411
|
|
|
* |
412
|
|
|
* @param int $y year (2003) |
413
|
|
|
* @param int $m month (9) |
414
|
|
|
* @param int $d day (4) |
415
|
|
|
* @param int $h hour |
416
|
|
|
* |
417
|
|
|
* @return int (60) |
418
|
|
|
* @access protected |
419
|
|
|
*/ |
420
|
|
|
function getMinutesInHour($y=null, $m=null, $d=null, $h=null) |
|
|
|
|
421
|
|
|
{ |
422
|
|
|
return 60; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Returns the number of seconds in a minutes |
427
|
|
|
* |
428
|
|
|
* @param int $y year (2003) |
429
|
|
|
* @param int $m month (9) |
430
|
|
|
* @param int $d day (4) |
431
|
|
|
* @param int $h hour |
432
|
|
|
* @param int $i minute |
433
|
|
|
* |
434
|
|
|
* @return int (60) |
435
|
|
|
* @access protected |
436
|
|
|
*/ |
437
|
|
|
function getSecondsInMinute($y=null, $m=null, $d=null, $h=null, $i=null) |
|
|
|
|
438
|
|
|
{ |
439
|
|
|
return 60; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Checks if the given day is the current day |
444
|
|
|
* |
445
|
|
|
* @param mixed $stamp Any timestamp format recognized by Pear::Date |
446
|
|
|
* |
447
|
|
|
* @return boolean |
448
|
|
|
* @access protected |
449
|
|
|
*/ |
450
|
|
|
function isToday($stamp) |
|
|
|
|
451
|
|
|
{ |
452
|
|
|
static $today = null; |
453
|
|
|
if (is_null($today)) { |
454
|
|
|
$today_date = @date('Y n j'); |
455
|
|
|
$today = sscanf($today_date, '%d %d %d'); |
456
|
|
|
} |
457
|
|
|
$date = UnixTS::stampCollection($stamp); |
|
|
|
|
458
|
|
|
return ( $date[2] == $today[2] |
459
|
|
|
&& $date[1] == $today[1] |
460
|
|
|
&& $date[0] == $today[0] |
461
|
|
|
); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.