| [ Index ] |
krapohl.info |
[Summary view] [Print] [Text view]
1 <?php 2 3 // The constant telling us what day starts the week. Monday (1) is the 4 // international standard. Redefine this to 0 if you want weeks to 5 // begin on Sunday. 6 //define('DATE_CALC_BEGIN_WEEKDAY', 1); 7 8 /** 9 * Date_Calc is a calendar class used to calculate and 10 * manipulate calendar dates and retrieve dates in a calendar 11 * format. It does not rely on 32-bit system date stamps, so 12 * you can display calendars and compare dates that date 13 * pre 1970 and post 2038. 14 * 15 * This source file is subject to version 2.02 of the PHP license, 16 * that is bundled with this package in the file LICENSE, and is 17 * available at through the world-wide-web at 18 * http://www.php.net/license/2_02.txt. 19 * If you did not receive a copy of the PHP license and are unable to 20 * obtain it through the world-wide-web, please send a note to 21 * license@php.net so we can mail you a copy immediately. 22 * 23 * Copyright (c) 1999, 2000 ispi 24 * 25 * @access public 26 * 27 * @version 1.2.5 28 * @author Monte Ohrt <monte@ispi.net> 29 */ 30 31 class Date_Calc { 32 33 /** 34 * Returns the current local date. NOTE: This function 35 * retrieves the local date using strftime(), which may 36 * or may not be 32-bit safe on your system. 37 * 38 * @param string the strftime() format to return the date 39 * 40 * @access public 41 * 42 * @return string the current date in specified format 43 */ 44 45 function dateNow($format="%Y%m%d") 46 { 47 return(strftime($format,time())); 48 49 } // end func dateNow 50 51 /** 52 * Returns true for valid date, false for invalid date. 53 * 54 * @param string year in format CCYY 55 * @param string month in format MM 56 * @param string day in format DD 57 * 58 * @access public 59 * 60 * @return boolean true/false 61 */ 62 63 function isValidDate($day, $month, $year) 64 { 65 66 if(empty($year) || empty($month) || empty($day)) 67 return false; 68 69 // must be digits only 70 if(preg_match("/\D/",$year)) 71 return false; 72 if(preg_match("/\D/",$month)) 73 return false; 74 if(preg_match("/\D/",$day)) 75 return false; 76 77 if($year < 0 || $year > 9999) 78 return false; 79 if($month < 1 || $month > 12) 80 return false; 81 if($day < 1 || $day > 31 || $day > Date_Calc::daysInMonth($month,$year)) 82 return false; 83 84 return true; 85 86 } // end func isValidDate 87 88 function isLeapYear($year="") 89 { 90 91 if(empty($year)) 92 $year = Date_Calc::dateNow("%Y"); 93 94 if(strlen($year) != 4) 95 return false; 96 97 if(preg_match("/\D/",$year)) 98 return false; 99 100 return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0); 101 102 } // end func isLeapYear 103 104 /** 105 * Determines if given date is a future date from now. 106 * 107 * @param string year in format CCYY 108 * @param string month in format MM 109 * @param string day in format DD 110 * 111 * @access public 112 * 113 * @return boolean true/false 114 */ 115 116 function isFutureDate($day,$month,$year) 117 { 118 $this_year = Date_Calc::dateNow("%Y"); 119 $this_month = Date_Calc::dateNow("%m"); 120 $this_day = Date_Calc::dateNow("%d"); 121 122 123 if($year > $this_year) 124 return true; 125 elseif($year == $this_year) 126 if($month > $this_month) 127 return true; 128 elseif($month == $this_month) 129 if($day > $this_day) 130 return true; 131 132 return false; 133 134 } // end func isFutureDate 135 136 /** 137 * Determines if given date is a past date from now. 138 * 139 * @param string year in format CCYY 140 * @param string month in format MM 141 * @param string day in format DD 142 * 143 * @access public 144 * 145 * @return boolean true/false 146 */ 147 148 function isPastDate($day,$month,$year) 149 { 150 $this_year = Date_Calc::dateNow("%Y"); 151 $this_month = Date_Calc::dateNow("%m"); 152 $this_day = Date_Calc::dateNow("%d"); 153 154 155 if($year < $this_year) 156 return true; 157 elseif($year == $this_year) 158 if($month < $this_month) 159 return true; 160 elseif($month == $this_month) 161 if($day < $this_day) 162 return true; 163 164 return false; 165 166 } // end func isPastDate 167 168 /** 169 * Returns day of week for given date, 0=Sunday 170 * 171 * @param string year in format CCYY, default is current local year 172 * @param string month in format MM, default is current local month 173 * @param string day in format DD, default is current local day 174 * 175 * @access public 176 * 177 * @return int $weekday_number 178 */ 179 180 function dayOfWeek($day="",$month="",$year="") 181 { 182 183 if(empty($year)) 184 $year = Date_Calc::dateNow("%Y"); 185 if(empty($month)) 186 $month = Date_Calc::dateNow("%m"); 187 if(empty($day)) 188 $day = Date_Calc::dateNow("%d"); 189 190 if($month > 2) 191 $month -= 2; 192 else 193 { 194 $month += 10; 195 $year--; 196 } 197 198 $day = ( floor((13 * $month - 1) / 5) + 199 $day + ($year % 100) + 200 floor(($year % 100) / 4) + 201 floor(($year / 100) / 4) - 2 * 202 floor($year / 100) + 77); 203 204 $weekday_number = (($day - 7 * floor($day / 7))); 205 206 return $weekday_number; 207 208 } // end func dayOfWeek 209 210 /** 211 * Returns week of the year, first Sunday is first day of first week 212 * 213 * @param string day in format DD 214 * @param string month in format MM 215 * @param string year in format CCYY 216 * 217 * @access public 218 * 219 * @return integer $week_number 220 */ 221 222 function weekOfYear($day,$month,$year) 223 { 224 if(empty($year)) 225 $year = Date_Calc::dateNow("%Y"); 226 if(empty($month)) 227 $month = Date_Calc::dateNow("%m"); 228 if(empty($day)) 229 $day = Date_Calc::dateNow("%d"); 230 231 $week_year = $year - 1501; 232 $week_day = $week_year * 365 + floor($week_year / 4) - 29872 + 1 233 - floor($week_year / 100) + floor(($week_year - 300) / 400); 234 235 $week_number = 236 ceil((Date_Calc::julianDate($day,$month,$year) + floor(($week_day + 4) % 7)) / 7); 237 238 return $week_number; 239 240 } // end func weekOfYear 241 242 /** 243 * Returns number of days since 31 December of year before given date. 244 * 245 * @param string year in format CCYY, default is current local year 246 * @param string month in format MM, default is current local month 247 * @param string day in format DD, default is current local day 248 * 249 * @access public 250 * 251 * @return int $julian 252 */ 253 254 function julianDate($day="",$month="",$year="") 255 { 256 if(empty($year)) 257 $year = Date_Calc::dateNow("%Y"); 258 if(empty($month)) 259 $month = Date_Calc::dateNow("%m"); 260 if(empty($day)) 261 $day = Date_Calc::dateNow("%d"); 262 263 $days = array(0,31,59,90,120,151,181,212,243,273,304,334); 264 265 $julian = ($days[$month - 1] + $day); 266 267 if($month > 2 && Date_Calc::isLeapYear($year)) 268 $julian++; 269 270 return($julian); 271 272 } // end func julianDate 273 274 /** 275 * Returns quarter of the year for given date 276 * 277 * @param string year in format CCYY, default current local year 278 * @param string month in format MM, default current local month 279 * @param string day in format DD, default current local day 280 * 281 * @access public 282 * 283 * @return int $year_quarter 284 */ 285 286 function quarterOfYear($day="",$month="",$year="") 287 { 288 if(empty($year)) 289 $year = Date_Calc::dateNow("%Y"); 290 if(empty($month)) 291 $month = Date_Calc::dateNow("%m"); 292 if(empty($day)) 293 $day = Date_Calc::dateNow("%d"); 294 295 $year_quarter = (intval(($month - 1) / 3 + 1)); 296 297 return $year_quarter; 298 299 } // end func quarterOfYear 300 301 /** 302 * Returns date of begin of next month of given date. 303 * 304 * @param string year in format CCYY, default current local year 305 * @param string month in format MM, default current local month 306 * @param string day in format DD, default current local day 307 * @param string format for returned date 308 * 309 * @access public 310 * 311 * @return string date in given format 312 */ 313 314 function beginOfNextMonth($day="",$month="",$year="",$format="%Y%m%d") 315 { 316 if(empty($year)) 317 $year = Date_Calc::dateNow("%Y"); 318 if(empty($month)) 319 $month = Date_Calc::dateNow("%m"); 320 if(empty($day)) 321 $day = Date_Calc::dateNow("%d"); 322 323 if($month < 12) 324 { 325 $month++; 326 $day=1; 327 } 328 else 329 { 330 $year++; 331 $month=1; 332 $day=1; 333 } 334 335 return Date_Calc::dateFormat($day,$month,$year,$format); 336 337 } // end func beginOfNextMonth 338 339 /** 340 * Returns date of the last day of next month of given date. 341 * 342 * @param string year in format CCYY, default current local year 343 * @param string month in format MM, default current local month 344 * @param string day in format DD, default current local day 345 * @param string format for returned date 346 * 347 * @access public 348 * 349 * @return string date in given format 350 */ 351 352 function endOfNextMonth($day="",$month="",$year="",$format="%Y%m%d") 353 { 354 if(empty($year)) 355 $year = Date_Calc::dateNow("%Y"); 356 if(empty($month)) 357 $month = Date_Calc::dateNow("%m"); 358 if(empty($day)) 359 $day = Date_Calc::dateNow("%d"); 360 361 362 if($month < 12) 363 { 364 $month++; 365 } 366 else 367 { 368 $year++; 369 $month=1; 370 } 371 372 $day = Date_Calc::daysInMonth($month,$year); 373 374 return Date_Calc::dateFormat($day,$month,$year,$format); 375 376 } // end func endOfNextMonth 377 378 /** 379 * Returns date of the first day of previous month of given date. 380 * 381 * @param string year in format CCYY, default current local year 382 * @param string month in format MM, default current local month 383 * @param string day in format DD, default current local day 384 * @param string format for returned date 385 * 386 * @access public 387 * 388 * @return string date in given format 389 */ 390 391 function beginOfPrevMonth($day="",$month="",$year="",$format="%Y%m%d") 392 { 393 if(empty($year)) 394 $year = Date_Calc::dateNow("%Y"); 395 if(empty($month)) 396 $month = Date_Calc::dateNow("%m"); 397 if(empty($day)) 398 $day = Date_Calc::dateNow("%d"); 399 400 if($month > 1) 401 { 402 $month--; 403 $day=1; 404 } 405 else 406 { 407 $year--; 408 $month=12; 409 $day=1; 410 } 411 412 return Date_Calc::dateFormat($day,$month,$year,$format); 413 414 } // end func beginOfPrevMonth 415 416 /** 417 * Returns date of the last day of previous month for given date. 418 * 419 * @param string year in format CCYY, default current local year 420 * @param string month in format MM, default current local month 421 * @param string day in format DD, default current local day 422 * @param string format for returned date 423 * 424 * @access public 425 * 426 * @return string date in given format 427 */ 428 429 function endOfPrevMonth($day="",$month="",$year="",$format="%Y%m%d") 430 { 431 if(empty($year)) 432 $year = Date_Calc::dateNow("%Y"); 433 if(empty($month)) 434 $month = Date_Calc::dateNow("%m"); 435 if(empty($day)) 436 $day = Date_Calc::dateNow("%d"); 437 438 if($month > 1) 439 { 440 $month--; 441 } 442 else 443 { 444 $year--; 445 $month=12; 446 } 447 448 $day = Date_Calc::daysInMonth($month,$year); 449 450 return Date_Calc::dateFormat($day,$month,$year,$format); 451 452 } // end func endOfPrevMonth 453 454 /** 455 * Returns date of the next weekday of given date, 456 * skipping from Friday to Monday. 457 * 458 * @param string year in format CCYY, default current local year 459 * @param string month in format MM, default current local month 460 * @param string day in format DD, default current local day 461 * @param string format for returned date 462 * 463 * @access public 464 * 465 * @return string date in given format 466 */ 467 468 function nextWeekday($day="",$month="",$year="",$format="%Y%m%d") 469 { 470 if(empty($year)) 471 $year = Date_Calc::dateNow("%Y"); 472 if(empty($month)) 473 $month = Date_Calc::dateNow("%m"); 474 if(empty($day)) 475 $day = Date_Calc::dateNow("%d"); 476 477 $days = Date_Calc::dateToDays($day,$month,$year); 478 479 if(Date_Calc::dayOfWeek($day,$month,$year) == 5) 480 $days += 3; 481 elseif(Date_Calc::dayOfWeek($day,$month,$year) == 6) 482 $days += 2; 483 else 484 $days += 1; 485 486 return(Date_Calc::daysToDate($days,$format)); 487 488 } // end func nextWeekday 489 490 /** 491 * Returns date of the previous weekday, 492 * skipping from Monday to Friday. 493 * 494 * @param string year in format CCYY, default current local year 495 * @param string month in format MM, default current local month 496 * @param string day in format DD, default current local day 497 * @param string format for returned date 498 * 499 * @access public 500 * 501 * @return string date in given format 502 */ 503 504 function prevWeekday($day="",$month="",$year="",$format="%Y%m%d") 505 { 506 if(empty($year)) 507 $year = Date_Calc::dateNow("%Y"); 508 if(empty($month)) 509 $month = Date_Calc::dateNow("%m"); 510 if(empty($day)) 511 $day = Date_Calc::dateNow("%d"); 512 513 $days = Date_Calc::dateToDays($day,$month,$year); 514 515 if(Date_Calc::dayOfWeek($day,$month,$year) == 1) 516 $days -= 3; 517 elseif(Date_Calc::dayOfWeek($day,$month,$year) == 0) 518 $days -= 2; 519 else 520 $days -= 1; 521 522 return(Date_Calc::daysToDate($days,$format)); 523 524 } // end func prevWeekday 525 526 /** 527 * Returns date of the next specific day of the week 528 * from the given date. 529 * 530 * @param int day of week, 0=Sunday 531 * @param string year in format CCYY, default current local year 532 * @param string month in format MM, default current local month 533 * @param string day in format DD, default current local day 534 * @param boolean onOrAfter if true and days are same, returns current day 535 * @param string format for returned date 536 * 537 * @access public 538 * 539 * @return string date in given format 540 */ 541 542 function nextDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",$onOrAfter=false) 543 { 544 if(empty($year)) 545 $year = Date_Calc::dateNow("%Y"); 546 if(empty($month)) 547 $month = Date_Calc::dateNow("%m"); 548 if(empty($day)) 549 $day = Date_Calc::dateNow("%d"); 550 551 $days = Date_Calc::dateToDays($day,$month,$year); 552 $curr_weekday = Date_Calc::dayOfWeek($day,$month,$year); 553 554 if($curr_weekday == $dow) 555 { 556 if(!$onOrAfter) 557 $days += 7; 558 } 559 elseif($curr_weekday > $dow) 560 $days += 7 - ( $curr_weekday - $dow ); 561 else 562 $days += $dow - $curr_weekday; 563 564 return(Date_Calc::daysToDate($days,$format)); 565 566 } // end func nextDayOfWeek 567 568 /** 569 * Returns date of the previous specific day of the week 570 * from the given date. 571 * 572 * @param int day of week, 0=Sunday 573 * @param string year in format CCYY, default current local year 574 * @param string month in format MM, default current local month 575 * @param string day in format DD, default current local day 576 * @param boolean onOrBefore if true and days are same, returns current day 577 * @param string format for returned date 578 * 579 * @access public 580 * 581 * @return string date in given format 582 */ 583 584 function prevDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",$onOrBefore=false) 585 { 586 if(empty($year)) 587 $year = Date_Calc::dateNow("%Y"); 588 if(empty($month)) 589 $month = Date_Calc::dateNow("%m"); 590 if(empty($day)) 591 $day = Date_Calc::dateNow("%d"); 592 593 $days = Date_Calc::dateToDays($day,$month,$year); 594 $curr_weekday = Date_Calc::dayOfWeek($day,$month,$year); 595 596 if($curr_weekday == $dow) 597 { 598 if(!$onOrBefore) 599 $days -= 7; 600 } 601 elseif($curr_weekday < $dow) 602 $days -= 7 - ( $dow - $curr_weekday ); 603 else 604 $days -= $curr_weekday - $dow; 605 606 return(