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