[17073] | 1 | <?php |
---|
| 2 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |
---|
[23143] | 3 | // |
---|
| 4 | // +----------------------------------------------------------------------+ |
---|
| 5 | // | PHP Version 4 | |
---|
| 6 | // +----------------------------------------------------------------------+ |
---|
| 7 | // | Copyright (c) 1997-2002 The PHP Group | |
---|
| 8 | // +----------------------------------------------------------------------+ |
---|
| 9 | // | This source file is subject to version 2.02 of the PHP license, | |
---|
| 10 | // | that is bundled with this package in the file LICENSE, and is | |
---|
| 11 | // | available at through the world-wide-web at | |
---|
| 12 | // | http://www.php.net/license/3_0.txt. | |
---|
| 13 | // | If you did not receive a copy of the PHP license and are unable to | |
---|
| 14 | // | obtain it through the world-wide-web, please send a note to | |
---|
| 15 | // | license@php.net so we can mail you a copy immediately. | |
---|
| 16 | // +----------------------------------------------------------------------+ |
---|
| 17 | // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | |
---|
| 18 | // +----------------------------------------------------------------------+ |
---|
| 19 | // |
---|
| 20 | // $Id: PearDate.php,v 1.8 2004/08/20 20:00:55 quipo Exp $ |
---|
| 21 | // |
---|
[17073] | 22 | /** |
---|
[23143] | 23 | * @package Calendar |
---|
| 24 | * @version $Id: PearDate.php,v 1.8 2004/08/20 20:00:55 quipo Exp $ |
---|
[17073] | 25 | */ |
---|
| 26 | /** |
---|
| 27 | * Load PEAR::Date class |
---|
| 28 | */ |
---|
| 29 | require_once 'Date.php'; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Performs calendar calculations based on the PEAR::Date class |
---|
| 33 | * Timestamps are in the ISO-8601 format (YYYY-MM-DD HH:MM:SS) |
---|
[23143] | 34 | * @package Calendar |
---|
[17073] | 35 | * @access protected |
---|
| 36 | */ |
---|
| 37 | class Calendar_Engine_PearDate /* implements Calendar_Engine_Interface */ |
---|
| 38 | { |
---|
| 39 | /** |
---|
| 40 | * Makes sure a given timestamp is only ever parsed once |
---|
| 41 | * Uses a static variable to prevent date() being used twice |
---|
| 42 | * for a date which is already known |
---|
[23143] | 43 | * @param mixed Any timestamp format recognized by Pear::Date |
---|
[17073] | 44 | * @return object Pear::Date object |
---|
| 45 | * @access protected |
---|
| 46 | */ |
---|
| 47 | function stampCollection($stamp) |
---|
| 48 | { |
---|
| 49 | static $stamps = array(); |
---|
| 50 | if (!isset($stamps[$stamp])) { |
---|
| 51 | $stamps[$stamp] = new Date($stamp); |
---|
| 52 | } |
---|
| 53 | return $stamps[$stamp]; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Returns a numeric year given a iso-8601 datetime |
---|
[23143] | 58 | * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
---|
[17073] | 59 | * @return int year (e.g. 2003) |
---|
| 60 | * @access protected |
---|
| 61 | */ |
---|
| 62 | function stampToYear($stamp) |
---|
| 63 | { |
---|
| 64 | $date = Calendar_Engine_PearDate::stampCollection($stamp); |
---|
| 65 | return (int)$date->year; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Returns a numeric month given a iso-8601 datetime |
---|
[23143] | 70 | * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
---|
[17073] | 71 | * @return int month (e.g. 9) |
---|
| 72 | * @access protected |
---|
| 73 | */ |
---|
| 74 | function stampToMonth($stamp) |
---|
| 75 | { |
---|
| 76 | $date = Calendar_Engine_PearDate::stampCollection($stamp); |
---|
| 77 | return (int)$date->month; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Returns a numeric day given a iso-8601 datetime |
---|
[23143] | 82 | * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
---|
[17073] | 83 | * @return int day (e.g. 15) |
---|
| 84 | * @access protected |
---|
| 85 | */ |
---|
| 86 | function stampToDay($stamp) |
---|
| 87 | { |
---|
| 88 | $date = Calendar_Engine_PearDate::stampCollection($stamp); |
---|
| 89 | return (int)$date->day; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Returns a numeric hour given a iso-8601 datetime |
---|
[23143] | 94 | * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
---|
[17073] | 95 | * @return int hour (e.g. 13) |
---|
| 96 | * @access protected |
---|
| 97 | */ |
---|
| 98 | function stampToHour($stamp) |
---|
| 99 | { |
---|
| 100 | $date = Calendar_Engine_PearDate::stampCollection($stamp); |
---|
| 101 | return (int)$date->hour; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Returns a numeric minute given a iso-8601 datetime |
---|
[23143] | 106 | * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
---|
[17073] | 107 | * @return int minute (e.g. 34) |
---|
| 108 | * @access protected |
---|
| 109 | */ |
---|
| 110 | function stampToMinute($stamp) |
---|
| 111 | { |
---|
| 112 | $date = Calendar_Engine_PearDate::stampCollection($stamp); |
---|
| 113 | return (int)$date->minute; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Returns a numeric second given a iso-8601 datetime |
---|
[23143] | 118 | * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
---|
[17073] | 119 | * @return int second (e.g. 51) |
---|
| 120 | * @access protected |
---|
| 121 | */ |
---|
| 122 | function stampToSecond($stamp) |
---|
| 123 | { |
---|
| 124 | $date = Calendar_Engine_PearDate::stampCollection($stamp); |
---|
| 125 | return (int)$date->second; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Returns a iso-8601 datetime |
---|
[23143] | 130 | * @param int year (2003) |
---|
| 131 | * @param int month (9) |
---|
| 132 | * @param int day (13) |
---|
| 133 | * @param int hour (13) |
---|
| 134 | * @param int minute (34) |
---|
| 135 | * @param int second (53) |
---|
[17073] | 136 | * @return string iso-8601 datetime |
---|
| 137 | * @access protected |
---|
| 138 | */ |
---|
| 139 | function dateToStamp($y, $m, $d, $h=0, $i=0, $s=0) |
---|
| 140 | { |
---|
| 141 | $r = array(); |
---|
| 142 | Calendar_Engine_PearDate::adjustDate($y, $m, $d, $h, $i, $s); |
---|
| 143 | $key = $y.$m.$d.$h.$i.$s; |
---|
| 144 | if (!isset($r[$key])) { |
---|
| 145 | $r[$key] = sprintf("%04d-%02d-%02d %02d:%02d:%02d", |
---|
| 146 | $y, $m, $d, $h, $i, $s); |
---|
| 147 | } |
---|
| 148 | return $r[$key]; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Set the correct date values (useful for math operations on dates) |
---|
[23143] | 153 | * @param int year (2003) |
---|
| 154 | * @param int month (9) |
---|
| 155 | * @param int day (13) |
---|
| 156 | * @param int hour (13) |
---|
| 157 | * @param int minute (34) |
---|
| 158 | * @param int second (53) |
---|
[17073] | 159 | * @access protected |
---|
| 160 | */ |
---|
| 161 | function adjustDate(&$y, &$m, &$d, &$h, &$i, &$s) |
---|
| 162 | { |
---|
| 163 | if ($s < 0) { |
---|
| 164 | $m -= floor($s / 60); |
---|
| 165 | $s = -$s % 60; |
---|
| 166 | } |
---|
| 167 | if ($s > 60) { |
---|
| 168 | $m += floor($s / 60); |
---|
| 169 | $s %= 60; |
---|
| 170 | } |
---|
| 171 | if ($i < 0) { |
---|
| 172 | $h -= floor($i / 60); |
---|
| 173 | $i = -$i % 60; |
---|
| 174 | } |
---|
| 175 | if ($i > 60) { |
---|
| 176 | $h += floor($i / 60); |
---|
| 177 | $i %= 60; |
---|
| 178 | } |
---|
| 179 | if ($h < 0) { |
---|
| 180 | $d -= floor($h / 24); |
---|
| 181 | $h = -$h % 24; |
---|
| 182 | } |
---|
| 183 | if ($h > 24) { |
---|
| 184 | $d += floor($h / 24); |
---|
| 185 | $h %= 24; |
---|
| 186 | } |
---|
| 187 | for(; $m < 1; $y--, $m+=12); |
---|
| 188 | for(; $m > 12; $y++, $m-=12); |
---|
| 189 | |
---|
| 190 | while ($d < 1) { |
---|
| 191 | if ($m > 1) { |
---|
| 192 | $m--; |
---|
| 193 | } else { |
---|
| 194 | $m = 12; |
---|
| 195 | $y--; |
---|
| 196 | } |
---|
| 197 | $d += Date_Calc::daysInMonth($m, $y); |
---|
| 198 | } |
---|
| 199 | for ($max_days = Date_Calc::daysInMonth($m, $y); $d > $max_days; ) { |
---|
| 200 | $d -= $max_days; |
---|
| 201 | if ($m < 12) { |
---|
| 202 | $m++; |
---|
| 203 | } else { |
---|
| 204 | $m = 1; |
---|
| 205 | $y++; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * The upper limit on years that the Calendar Engine can work with |
---|
| 212 | * @return int 9999 |
---|
| 213 | * @access protected |
---|
| 214 | */ |
---|
| 215 | function getMaxYears() |
---|
| 216 | { |
---|
| 217 | return 9999; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | * The lower limit on years that the Calendar Engine can work with |
---|
| 222 | * @return int 0 |
---|
| 223 | * @access protected |
---|
| 224 | */ |
---|
| 225 | function getMinYears() |
---|
| 226 | { |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * Returns the number of months in a year |
---|
| 232 | * @return int (12) |
---|
| 233 | * @access protected |
---|
| 234 | */ |
---|
| 235 | function getMonthsInYear($y=null) |
---|
| 236 | { |
---|
| 237 | return 12; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * Returns the number of days in a month, given year and month |
---|
[23143] | 242 | * @param int year (2003) |
---|
| 243 | * @param int month (9) |
---|
[17073] | 244 | * @return int days in month |
---|
| 245 | * @access protected |
---|
| 246 | */ |
---|
| 247 | function getDaysInMonth($y, $m) |
---|
| 248 | { |
---|
| 249 | return (int)Date_Calc::daysInMonth($m, $y); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * Returns numeric representation of the day of the week in a month, |
---|
| 254 | * given year and month |
---|
[23143] | 255 | * @param int year (2003) |
---|
| 256 | * @param int month (9) |
---|
[17073] | 257 | * @return int from 0 to 7 |
---|
| 258 | * @access protected |
---|
| 259 | */ |
---|
| 260 | function getFirstDayInMonth($y, $m) |
---|
| 261 | { |
---|
| 262 | return (int)Date_Calc::dayOfWeek(1, $m, $y); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Returns the number of days in a week |
---|
[23143] | 267 | * @param int year (2003) |
---|
| 268 | * @param int month (9) |
---|
| 269 | * @param int day (4) |
---|
[17073] | 270 | * @return int (7) |
---|
| 271 | * @access protected |
---|
| 272 | */ |
---|
[23143] | 273 | function getDaysInWeek($y=NULL, $m=NULL, $d=NULL) |
---|
[17073] | 274 | { |
---|
| 275 | return 7; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
| 279 | * Returns the number of the week in the year (ISO-8601), given a date |
---|
[23143] | 280 | * @param int year (2003) |
---|
| 281 | * @param int month (9) |
---|
| 282 | * @param int day (4) |
---|
[17073] | 283 | * @return int week number |
---|
| 284 | * @access protected |
---|
| 285 | */ |
---|
| 286 | function getWeekNInYear($y, $m, $d) |
---|
| 287 | { |
---|
[23143] | 288 | return Date_Calc::weekOfYear($d, $m, $y); //beware, Date_Calc doesn't follow ISO-8601 standard! |
---|
[17073] | 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * Returns the number of the week in the month, given a date |
---|
[23143] | 293 | * @param int year (2003) |
---|
| 294 | * @param int month (9) |
---|
| 295 | * @param int day (4) |
---|
| 296 | * @param int first day of the week (default: monday) |
---|
[17073] | 297 | * @return int week number |
---|
| 298 | * @access protected |
---|
| 299 | */ |
---|
| 300 | function getWeekNInMonth($y, $m, $d, $firstDay=1) |
---|
| 301 | { |
---|
| 302 | $weekEnd = ($firstDay == 0) ? $this->getDaysInWeek()-1 : $firstDay-1; |
---|
| 303 | $end_of_week = (int)Date_Calc::nextDayOfWeek($weekEnd, 1, $m, $y, '%e', true); |
---|
| 304 | $w = 1; |
---|
| 305 | while ($d > $end_of_week) { |
---|
| 306 | ++$w; |
---|
| 307 | $end_of_week += $this->getDaysInWeek(); |
---|
| 308 | } |
---|
| 309 | return $w; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | * Returns the number of weeks in the month |
---|
[23143] | 314 | * @param int year (2003) |
---|
| 315 | * @param int month (9) |
---|
| 316 | * @param int first day of the week (default: monday) |
---|
[17073] | 317 | * @return int weeks number |
---|
| 318 | * @access protected |
---|
| 319 | */ |
---|
| 320 | function getWeeksInMonth($y, $m, $firstDay=1) |
---|
| 321 | { |
---|
| 322 | $FDOM = Date_Calc::firstOfMonthWeekday($m, $y); |
---|
| 323 | if ($FDOM == 0) { |
---|
| 324 | $FDOM = $this->getDaysInWeek(); |
---|
| 325 | } |
---|
| 326 | if ($FDOM > $firstDay) { |
---|
| 327 | $daysInTheFirstWeek = $this->getDaysInWeek() - $FDOM + $firstDay; |
---|
| 328 | $weeks = 1; |
---|
| 329 | } else { |
---|
| 330 | $daysInTheFirstWeek = $firstDay - $FDOM; |
---|
| 331 | $weeks = 0; |
---|
| 332 | } |
---|
| 333 | $daysInTheFirstWeek %= $this->getDaysInWeek(); |
---|
| 334 | return (int)(ceil(($this->getDaysInMonth($y, $m) - $daysInTheFirstWeek) / |
---|
| 335 | $this->getDaysInWeek()) + $weeks); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | /** |
---|
| 339 | * Returns the number of the day of the week (0=sunday, 1=monday...) |
---|
[23143] | 340 | * @param int year (2003) |
---|
| 341 | * @param int month (9) |
---|
| 342 | * @param int day (4) |
---|
[17073] | 343 | * @return int weekday number |
---|
| 344 | * @access protected |
---|
| 345 | */ |
---|
| 346 | function getDayOfWeek($y, $m, $d) |
---|
| 347 | { |
---|
| 348 | return Date_Calc::dayOfWeek($d, $m, $y); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | /** |
---|
| 352 | * Returns a list of integer days of the week beginning 0 |
---|
[23143] | 353 | * @param int year (2003) |
---|
| 354 | * @param int month (9) |
---|
| 355 | * @param int day (4) |
---|
[17073] | 356 | * @return array (0, 1, 2, 3, 4, 5, 6) 1 = Monday |
---|
| 357 | * @access protected |
---|
| 358 | */ |
---|
[23143] | 359 | function getWeekDays($y=NULL, $m=NULL, $d=NULL) |
---|
[17073] | 360 | { |
---|
| 361 | return array(0, 1, 2, 3, 4, 5, 6); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | /** |
---|
| 365 | * Returns the default first day of the week |
---|
[23143] | 366 | * @param int year (2003) |
---|
| 367 | * @param int month (9) |
---|
| 368 | * @param int day (4) |
---|
[17073] | 369 | * @return int (default 1 = Monday) |
---|
| 370 | * @access protected |
---|
| 371 | */ |
---|
[23143] | 372 | function getFirstDayOfWeek($y=NULL, $m=NULL, $d=NULL) |
---|
[17073] | 373 | { |
---|
| 374 | return 1; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
| 378 | * Returns the number of hours in a day |
---|
| 379 | * @return int (24) |
---|
| 380 | * @access protected |
---|
| 381 | */ |
---|
| 382 | function getHoursInDay($y=null,$m=null,$d=null) |
---|
| 383 | { |
---|
| 384 | return 24; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | * Returns the number of minutes in an hour |
---|
| 389 | * @return int (60) |
---|
| 390 | * @access protected |
---|
| 391 | */ |
---|
| 392 | function getMinutesInHour($y=null,$m=null,$d=null,$h=null) |
---|
| 393 | { |
---|
| 394 | return 60; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /** |
---|
| 398 | * Returns the number of seconds in a minutes |
---|
| 399 | * @return int (60) |
---|
| 400 | * @access protected |
---|
| 401 | */ |
---|
| 402 | function getSecondsInMinute($y=null,$m=null,$d=null,$h=null,$i=null) |
---|
| 403 | { |
---|
| 404 | return 60; |
---|
| 405 | } |
---|
| 406 | } |
---|
[23143] | 407 | ?> |
---|