| 1 |
<?php |
| 2 |
// |
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | PHP Version 4 | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | Copyright (c) 1997-2002 The PHP Group | |
| 7 |
// +----------------------------------------------------------------------+ |
| 8 |
// | This source file is subject to version 2.02 of the PHP license, | |
| 9 |
// | that is bundled with this package in the file LICENSE, and is | |
| 10 |
// | available at through the world-wide-web at | |
| 11 |
// | http://www.php.net/license/2_02.txt. | |
| 12 |
// | If you did not receive a copy of the PHP license and are unable to | |
| 13 |
// | obtain it through the world-wide-web, please send a note to | |
| 14 |
// | license@php.net so we can mail you a copy immediately. | |
| 15 |
// +----------------------------------------------------------------------+ |
| 16 |
// | Authors: Baba Buehler <baba@babaz.com> | |
| 17 |
// | | |
| 18 |
// +----------------------------------------------------------------------+ |
| 19 |
// |
| 20 |
// $Id: Date.php,v 1.6 2002/08/14 06:39:25 baba Exp $ |
| 21 |
// |
| 22 |
// Date Class |
| 23 |
// |
| 24 |
require_once 'Date/TimeZone.php'; |
| 25 |
require_once 'Date/Calc.php'; |
| 26 |
|
| 27 |
/** |
| 28 |
* "YYYY-MM-DD HH:MM:SS" |
| 29 |
*/ |
| 30 |
define('DATE_FORMAT_ISO', 1); |
| 31 |
/** |
| 32 |
* "YYYYMMDDHHMMSS" |
| 33 |
*/ |
| 34 |
define('DATE_FORMAT_TIMESTAMP', 2); |
| 35 |
/** |
| 36 |
* long int, seconds since the unix epoch |
| 37 |
*/ |
| 38 |
define('DATE_FORMAT_UNIXTIME', 3); |
| 39 |
|
| 40 |
define('DATE_FORMAT_ENGLISH_SHORT', 4); |
| 41 |
define('DATE_FORMAT_GERMAN_SHORT', 5); |
| 42 |
|
| 43 |
/** |
| 44 |
* Generic date handling class for PEAR. |
| 45 |
* |
| 46 |
* Generic date handling class for PEAR. Attempts to be time zone aware |
| 47 |
* through the Date::TimeZone class. Supports several operations from |
| 48 |
* Date::Calc on Date objects. |
| 49 |
* |
| 50 |
* @author Baba Buehler <baba@babaz.com> |
| 51 |
* @package Date |
| 52 |
* @access public |
| 53 |
* @version 1.1 |
| 54 |
*/ |
| 55 |
class Date |
| 56 |
{ |
| 57 |
/** |
| 58 |
* the year |
| 59 |
* @var int |
| 60 |
*/ |
| 61 |
var $year; |
| 62 |
/** |
| 63 |
* the month |
| 64 |
* @var int |
| 65 |
*/ |
| 66 |
var $month; |
| 67 |
/** |
| 68 |
* the day |
| 69 |
* @var int |
| 70 |
*/ |
| 71 |
var $day; |
| 72 |
/** |
| 73 |
* the hour |
| 74 |
* @var int |
| 75 |
*/ |
| 76 |
var $hour; |
| 77 |
/** |
| 78 |
* the minute |
| 79 |
* @var int |
| 80 |
*/ |
| 81 |
var $minute; |
| 82 |
/** |
| 83 |
* the second |
| 84 |
* @var int |
| 85 |
*/ |
| 86 |
var $second; |
| 87 |
/** |
| 88 |
* timezone for this date |
| 89 |
* @var object Date_TimeZone |
| 90 |
*/ |
| 91 |
var $tz; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Constructor |
| 96 |
* |
| 97 |
* Creates a new Date Object |
| 98 |
* initialized to the current date/time in the |
| 99 |
* system default time zone by default. A date optionally |
| 100 |
* passed in may be in the ISO, TIMESTAMP or UNIXTIME format, |
| 101 |
* or another Date object. |
| 102 |
* |
| 103 |
* @access public |
| 104 |
* @param mixed $date optional - date/time to initialize |
| 105 |
* @return object Date the new Date object |
| 106 |
*/ |
| 107 |
function Date($date = null) { |
| 108 |
$this->tz = Date_TimeZone::getDefault(); |
| 109 |
if (is_null($date)) { |
| 110 |
$this->setDate(date('Y-m-d H:i:s')); |
| 111 |
} |
| 112 |
elseif (is_object($date) && (get_class($date) == 'date')) { |
| 113 |
$this->copy($date); |
| 114 |
} |
| 115 |
elseif (preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $date)) { |
| 116 |
$this->setDate($date); |
| 117 |
} |
| 118 |
elseif (preg_match('/\d{14}/',$date)) { |
| 119 |
$this->setDate($date,DATE_FORMAT_TIMESTAMP); |
| 120 |
} |
| 121 |
elseif (preg_match('/\d{4}-\d{2}-\d{2}/', $date)) { // DATE_FORMAT_ENGLISH_SHORT |
| 122 |
$this->setDate($date . " " . "00:00:00"); |
| 123 |
} |
| 124 |
elseif (preg_match('/(\d+)\.(\d+)\.(\d+)/', $date, $matches)) { // DATE_FORMAT_GERMAN_SHORT |
| 125 |
$date = join("-", array($matches[3], $matches[2], $matches[1])) . " " . "00:00:00"; |
| 126 |
$this->setDate($date); |
| 127 |
} |
| 128 |
else { |
| 129 |
$this->setDate($date,DATE_FORMAT_UNIXTIME); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Set the fields of a Date object based on the input date and format |
| 135 |
* |
| 136 |
* Set the fields of a Date object based on the input date and format, |
| 137 |
* which is specified by the DATE_FORMAT_* constants. |
| 138 |
* |
| 139 |
* @access public |
| 140 |
* @param string $date input date |
| 141 |
* @param int $format format constant (DATE_FORMAT_*) of the input date |
| 142 |
*/ |
| 143 |
function setDate($date, $format = DATE_FORMAT_ISO) |
| 144 |
{ |
| 145 |
switch($format) { |
| 146 |
case DATE_FORMAT_ISO: |
| 147 |
list($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second) = |
| 148 |
sscanf($date, "%04u-%02u-%02u %02u:%02u:%02u"); |
| 149 |
break; |
| 150 |
case DATE_FORMAT_TIMESTAMP: |
| 151 |
list($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second) = |
| 152 |
sscanf($date, "%04u%02u%02u%02u%02u%02u"); |
| 153 |
break; |
| 154 |
case DATE_FORMAT_UNIXTIME: |
| 155 |
$this->setDate(date("Y-m-d H:i:s", $date)); |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get a string (or other) representation of this date |
| 162 |
* |
| 163 |
* Get a string (or other) representation of this date in the |
| 164 |
* format specified by the DATE_FORMAT_* constants. |
| 165 |
* |
| 166 |
* @access public |
| 167 |
* @param int $format format constant (DATE_FORMAT_*) of the output date |
| 168 |
* @return string the date in the requested format |
| 169 |
*/ |
| 170 |
function getDate($format = DATE_FORMAT_ISO) |
| 171 |
{ |
| 172 |
switch($format) { |
| 173 |
case DATE_FORMAT_ISO: |
| 174 |
return $this->format("%Y-%m-%d %T"); |
| 175 |
break; |
| 176 |
case DATE_FORMAT_TIMESTAMP: |
| 177 |
return $this->format("%Y%m%d%H%M%S"); |
| 178 |
break; |
| 179 |
case DATE_FORMAT_UNIXTIME: |
| 180 |
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year, 0); |
| 181 |
break; |
| 182 |
case DATE_FORMAT_ENGLISH_SHORT: |
| 183 |
return $this->format("%Y-%m-%d"); |
| 184 |
break; |
| 185 |
case DATE_FORMAT_GERMAN_SHORT: |
| 186 |
return $this->format("%d.%m.%Y"); |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Copy values from another Date object |
| 193 |
* |
| 194 |
* Makes this Date a copy of another Date object. |
| 195 |
* |
| 196 |
* @access public |
| 197 |
* @param object Date $date Date to copy from |
| 198 |
*/ |
| 199 |
function copy($date) |
| 200 |
{ |
| 201 |
$this->year = $date->year; |
| 202 |
$this->month = $date->month; |
| 203 |
$this->day = $date->day; |
| 204 |
$this->hour = $date->hour; |
| 205 |
$this->minute = $date->minute; |
| 206 |
$this->second = $date->second; |
| 207 |
$this->tz = $date->tz; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Date pretty printing, similar to strftime() |
| 212 |
* |
| 213 |
* Formats the date in the given format, much like |
| 214 |
* strftime(). Most strftime() options are supported.<br><br> |
| 215 |
* |
| 216 |
* formatting options:<br><br> |
| 217 |
* |
| 218 |
* <code>%a </code> abbreviated weekday name (Sun, Mon, Tue) <br> |
| 219 |
* <code>%A </code> full weekday name (Sunday, Monday, Tuesday) <br> |
| 220 |
* <code>%b </code> abbreviated month name (Jan, Feb, Mar) <br> |
| 221 |
* <code>%B </code> full month name (January, February, March) <br> |
| 222 |
* <code>%C </code> century number (the year divided by 100 and truncated to an integer, range 00 to 99) <br> |
| 223 |
* <code>%d </code> day of month (range 00 to 31) <br> |
| 224 |
* <code>%D </code> same as "%m/%d/%y" <br> |
| 225 |
* <code>%e </code> day of month, single digit (range 0 to 31) <br> |
| 226 |
* <code>%E </code> number of days since unspecified epoch (integer, Date_Calc::dateToDays()) <br> |
| 227 |
* <code>%H </code> hour as decimal number (00 to 23) <br> |
| 228 |
* <code>%I </code> hour as decimal number on 12-hour clock (01 to 12) <br> |
| 229 |
* <code>%j </code> day of year (range 001 to 366) <br> |
| 230 |
* <code>%m </code> month as decimal number (range 01 to 12) <br> |
| 231 |
* <code>%M </code> minute as a decimal number (00 to 59) <br> |
| 232 |
* <code>%n </code> newline character (\n) <br> |
| 233 |
* <code>%O </code> dst-corrected timezone offset expressed as "+/-HH:MM" <br> |
| 234 |
* <code>%o </code> raw timezone offset expressed as "+/-HH:MM" <br> |
| 235 |
* <code>%p </code> either 'am' or 'pm' depending on the time <br> |
| 236 |
* <code>%P </code> either 'AM' or 'PM' depending on the time <br> |
| 237 |
* <code>%r </code> time in am/pm notation, same as "%I:%M:%S %p" <br> |
| 238 |
* <code>%R </code> time in 24-hour notation, same as "%H:%M" <br> |
| 239 |
* <code>%S </code> seconds as a decimal number (00 to 59) <br> |
| 240 |
* <code>%t </code> tab character (\t) <br> |
| 241 |
* <code>%T </code> current time, same as "%H:%M:%S" <br> |
| 242 |
* <code>%w </code> weekday as decimal (0 = Sunday) <br> |
| 243 |
* <code>%U </code> week number of current year, first sunday as first week <br> |
| 244 |
* <code>%y </code> year as decimal (range 00 to 99) <br> |
| 245 |
* <code>%Y </code> year as decimal including century (range 0000 to 9999) <br> |
| 246 |
* <code>%% </code> literal '%' <br> |
| 247 |
* <br> |
| 248 |
* |
| 249 |
* @access public |
| 250 |
* @param string format the format string for returned date/time |
| 251 |
* @return string date/time in given format |
| 252 |
*/ |
| 253 |
function format($format) |
| 254 |
{ |
| 255 |
$output = ""; |
| 256 |
|
| 257 |
for($strpos = 0; $strpos < strlen($format); $strpos++) { |
| 258 |
$char = substr($format,$strpos,1); |
| 259 |
if($char == "%") { |
| 260 |
$nextchar = substr($format,$strpos + 1,1); |
| 261 |
switch($nextchar) { |
| 262 |
case "a": |
| 263 |
$output .= Date_Calc::getWeekdayAbbrname($this->day,$this->month,$this->year); |
| 264 |
break; |
| 265 |
case "A": |
| 266 |
$output .= Date_Calc::getWeekdayFullname($this->day,$this->month,$this->year); |
| 267 |
break; |
| 268 |
case "b": |
| 269 |
$output .= Date_Calc::getMonthAbbrname($this->month); |
| 270 |
break; |
| 271 |
case "B": |
| 272 |
$output .= Date_Calc::getMonthFullname($this->month); |
| 273 |
break; |
| 274 |
case "C": |
| 275 |
$output .= sprintf("%02d",intval($this->year/100)); |
| 276 |
break; |
| 277 |
case "d": |
| 278 |
$output .= sprintf("%02d",$this->day); |
| 279 |
break; |
| 280 |
case "D": |
| 281 |
$output .= sprintf("%02d/%02d/%02d",$this->month,$this->day,$this->year); |
| 282 |
break; |
| 283 |
case "e": |
| 284 |
$output .= $this->day; |
| 285 |
break; |
| 286 |
case "E": |
| 287 |
$output .= Date_Calc::dateToDays($this->day,$this->month,$this->year); |
| 288 |
break; |
| 289 |
case "H": |
| 290 |
$output .= sprintf("%02d", $this->hour); |
| 291 |
break; |
| 292 |
case "I": |
| 293 |
$hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour; |
| 294 |
$output .= sprintf("%02d", $hour==0 ? 12 : $hour); |
| 295 |
break; |
| 296 |
case "j": |
| 297 |
$output .= Date_Calc::julianDate($this->day,$this->month,$this->year); |
| 298 |
break; |
| 299 |
case "m": |
| 300 |
$output .= sprintf("%02d",$this->month); |
| 301 |
break; |
| 302 |
case "M": |
| 303 |
$output .= sprintf("%02d",$this->minute); |
| 304 |
break; |
| 305 |
case "n": |
| 306 |
$output .= "\n"; |
| 307 |
break; |
| 308 |
case "O": |
| 309 |
$offms = $this->tz->getOffset($this); |
| 310 |
$direction = $offms >= 0 ? "+" : "-"; |
| 311 |
$offmins = abs($offms) / 1000 / 60; |
| 312 |
$hours = $offmins / 60; |
| 313 |
$minutes = $offmins % 60; |
| 314 |
$output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes); |
| 315 |
break; |
| 316 |
case "o": |
| 317 |
$offms = $this->tz->getRawOffset($this); |
| 318 |
$direction = $offms >= 0 ? "+" : "-"; |
| 319 |
$offmins = abs($offms) / 1000 / 60; |
| 320 |
$hours = $offmins / 60; |
| 321 |
$minutes = $offmins % 60; |
| 322 |
$output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes); |
| 323 |
break; |
| 324 |
case "p": |
| 325 |
$output .= $this->hour >= 12 ? "pm" : "am"; |
| 326 |
break; |
| 327 |
case "P": |
| 328 |
$output .= $this->hour >= 12 ? "PM" : "AM"; |
| 329 |
break; |
| 330 |
case "r": |
| 331 |
$hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour; |
| 332 |
$output .= sprintf("%02d:%02d:%02d %s", $hour==0 ? 12 : $hour, $this->minute, $this->second, $this->hour >= 12 ? "PM" : "AM"); |
| 333 |
break; |
| 334 |
case "R": |
| 335 |
$output .= sprintf("%02d:%02d", $this->hour, $this->minute); |
| 336 |
break; |
| 337 |
case "S": |
| 338 |
$output .= sprintf("%02d", $this->second); |
| 339 |
break; |
| 340 |
case "t": |
| 341 |
$output .= "\t"; |
| 342 |
break; |
| 343 |
case "T": |
| 344 |
$output .= sprintf("%02d:%02d:%02d", $this->hour, $this->minute, $this->second); |
| 345 |
break; |
| 346 |
case "w": |
| 347 |
$output .= Date_Calc::dayOfWeek($this->day,$this->month,$this->year); |
| 348 |
break; |
| 349 |
case "U": |
| 350 |
$output .= Date_Calc::weekOfYear($this->day,$this->month,$this->year); |
| 351 |
break; |
| 352 |
case "y": |
| 353 |
$output .= substr($this->year,2,2); |
| 354 |
break; |
| 355 |
case "Y": |
| 356 |
$output .= $this->year; |
| 357 |
break; |
| 358 |
case "Z": |
| 359 |
$output .= $this->tz->inDaylightTime($this) ? $this->tz->getDSTShortName() : $this->tz->getShortName(); |
| 360 |
break; |
| 361 |
case "%": |
| 362 |
$output .= "%"; |
| 363 |
break; |
| 364 |
default: |
| 365 |
$output .= $char.$nextchar; |
| 366 |
} |
| 367 |
$strpos++; |
| 368 |
} |
| 369 |
else { |
| 370 |
$output .= $char; |
| 371 |
} |
| 372 |
} |
| 373 |
return $output; |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Get this date/time in Unix time() format |
| 379 |
* |
| 380 |
* Get a representation of this date in Unix time() format. This may only be |
| 381 |
* valid for dates from 1970 to ~2038. |
| 382 |
* |
| 383 |
* @access public |
| 384 |
* @return int number of seconds since the unix epoch |
| 385 |
*/ |
| 386 |
function getTime() |
| 387 |
{ |
| 388 |
return $this->getDate(DATE_FORMAT_UNIXTIME); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Sets the time zone of this Date |
| 393 |
* |
| 394 |
* Sets the time zone of this date with the given |
| 395 |
* Date_TimeZone object. Does not alter the date/time, |
| 396 |
* only assigns a new time zone. For conversion, use |
| 397 |
* convertTZ(). |
| 398 |
* |
| 399 |
* @access public |
| 400 |
* @param object Date_TimeZone $tz the Date_TimeZone object to use |
| 401 |
*/ |
| 402 |
function setTZ($tz) |
| 403 |
{ |
| 404 |
$this->tz = $tz; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Sets the time zone of this date with the given time zone id |
| 409 |
* |
| 410 |
* Sets the time zone of this date with the given |
| 411 |
* time zone id, or to the system default if the |
| 412 |
* given id is invalid. Does not alter the date/time, |
| 413 |
* only assigns a new time zone. For conversion, use |
| 414 |
* convertTZ(). |
| 415 |
* |
| 416 |
* @access public |
| 417 |
* @param string id a time zone id |
| 418 |
*/ |
| 419 |
function setTZbyID($id) |
| 420 |
{ |
| 421 |
if(Date_TimeZone::isValidID($id)) { |
| 422 |
$this->tz = new Date_TimeZone($id); |
| 423 |
} else { |
| 424 |
$this->tz = Date_TimeZone::getDefault(); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Tests if this date/time is in DST |
| 430 |
* |
| 431 |
* Returns true if daylight savings time is in effect for |
| 432 |
* this date in this date's time zone. See Date_TimeZone::inDaylightTime() |
| 433 |
* for compatability information. |
| 434 |
* |
| 435 |
* @access public |
| 436 |
* @return boolean true if DST is in effect for this date |
| 437 |
*/ |
| 438 |
function inDaylightTime() |
| 439 |
{ |
| 440 |
return $this->tz->inDaylightTime($this); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Converts this date to UTC and sets this date's timezone to UTC |
| 445 |
* |
| 446 |
* Converts this date to UTC and sets this date's timezone to UTC |
| 447 |
* |
| 448 |
* @access public |
| 449 |
*/ |
| 450 |
function toUTC() |
| 451 |
{ |
| 452 |
if($this->tz->getOffset($this) > 0) { |
| 453 |
$this->subtractSeconds(intval($this->tz->getOffset($this) / 1000)); |
| 454 |
} else { |
| 455 |
$this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000)); |
| 456 |
} |
| 457 |
$this->tz = new Date_TimeZone('UTC'); |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Converts this date to a new time zone |
| 462 |
* |
| 463 |
* Converts this date to a new time zone. |
| 464 |
* WARNING: This may not work correctly if your system does not allow |
| 465 |
* putenv() or if localtime() does not work in your environment. See |
| 466 |
* Date::TimeZone::inDaylightTime() for more information. |
| 467 |
* |
| 468 |
* @access public |
| 469 |
* @param object Date_TimeZone $tz the Date::TimeZone object for the conversion time zone |
| 470 |
*/ |
| 471 |
function convertTZ($tz) |
| 472 |
{ |
| 473 |
// convert to UTC |
| 474 |
if($this->tz->getOffset($this) > 0) { |
| 475 |
$this->subtractSeconds(intval(abs($this->tz->getOffset($this)) / 1000)); |
| 476 |
} else { |
| 477 |
$this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000)); |
| 478 |
} |
| 479 |
// convert UTC to new timezone |
| 480 |
if($tz->getOffset($this) > 0) { |
| 481 |
$this->addSeconds(intval(abs($tz->getOffset($this)) / 1000)); |
| 482 |
} else { |
| 483 |
$this->subtractSeconds(intval(abs($tz->getOffset($this)) / 1000)); |
| 484 |
} |
| 485 |
$this->tz = $tz; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Converts this date to a new time zone, given a valid time zone ID |
| 490 |
* |
| 491 |
* Converts this date to a new time zone, given a valid time zone ID |
| 492 |
* WARNING: This may not work correctly if your system does not allow |
| 493 |
* putenv() or if localtime() does not work in your environment. See |
| 494 |
* Date::TimeZone::inDaylightTime() for more information. |
| 495 |
* |
| 496 |
* @access public |
| 497 |
* @param string id a time zone id |
| 498 |
*/ |
| 499 |
function convertTZbyID($id) |
| 500 |
{ |
| 501 |
if(Date_TimeZone::isValidID($id)) { |
| 502 |
$tz = new Date_TimeZone($id); |
| 503 |
} else { |
| 504 |
$tz = Date_TimeZone::getDefault(); |
| 505 |
} |
| 506 |
$this->convertTZ($tz); |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Adds a given number of seconds to the date |
| 511 |
* |
| 512 |
* Adds a given number of seconds to the date |
| 513 |
* |
| 514 |
* @access public |
| 515 |
* @param int $sec the number of seconds to add |
| 516 |
*/ |
| 517 |
function addSeconds($sec) |
| 518 |
{ |
| 519 |
$days = intval($sec/86400); |
| 520 |
$sec -= $days*86400; |
| 521 |
$hours = intval($sec/3600); |
| 522 |
$sec -= $hours*3600; |
| 523 |
$minutes = intval($sec/60); |
| 524 |
$sec -= $minutes*60; |
| 525 |
|
| 526 |
$this->second += $sec; |
| 527 |
if($this->second >= 60) { |
| 528 |
$this->minute++; |
| 529 |
$this->second -= 60; |
| 530 |
} |
| 531 |
|
| 532 |
$this->minute += $minutes; |
| 533 |
if($this->minute >= 60) { |
| 534 |
$this->hour++; |
| 535 |
if($this->hour >= 24) { |
| 536 |
list($this->year, $this->month, $this->day) = |
| 537 |
sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s"); |
| 538 |
$this->hour -= 24; |
| 539 |
} |
| 540 |
$this->minute -= 60; |
| 541 |
} |
| 542 |
|
| 543 |
$this->hour += $hours; |
| 544 |
if($this->hour >= 24) { |
| 545 |
list($this->year, $this->month, $this->day) = |
| 546 |
sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s"); |
| 547 |
$this->hour -= 24; |
| 548 |
} |
| 549 |
|
| 550 |
$d = Date_Calc::dateToDays($this->day, $this->month, $this->year); |
| 551 |
$d += $days; |
| 552 |
|
| 553 |
list($this->year, $this->month, $this->day) = |
| 554 |
sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s"); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Subtracts a given number of seconds from the date |
| 559 |
* |
| 560 |
* Subtracts a given number of seconds from the date |
| 561 |
* |
| 562 |
* @access public |
| 563 |
* @param int $sec the number of seconds to subtract |
| 564 |
*/ |
| 565 |
function subtractSeconds($sec) |
| 566 |
{ |
| 567 |
$days = intval($sec/86400); |
| 568 |
$sec -= $days*86400; |
| 569 |
$hours = intval($sec/3600); |
| 570 |
$sec -= $hours*3600; |
| 571 |
$minutes = intval($sec/60); |
| 572 |
$sec -= $minutes*60; |
| 573 |
|
| 574 |
$this->second -= $sec; |
| 575 |
if($this->second < 0) { |
| 576 |
$this->minute--; |
| 577 |
$this->second += 60; |
| 578 |
} |
| 579 |
|
| 580 |
$this->minute -= $minutes; |
| 581 |
if($this->minute < 0) { |
| 582 |
$this->hour--; |
| 583 |
if($this->hour < 0) { |
| 584 |
list($this->year, $this->month, $this->day) = |
| 585 |
sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); |
| 586 |
$this->hour += 24; |
| 587 |
} |
| 588 |
$this->minute += 60; |
| 589 |
} |
| 590 |
|
| 591 |
$this->hour -= $hours; |
| 592 |
if($this->hour < 0) { |
| 593 |
list($this->year, $this->month, $this->day) = |
| 594 |
sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); |
| 595 |
$this->hour += 24; |
| 596 |
} |
| 597 |
|
| 598 |
$d = Date_Calc::dateToDays($this->day, $this->month, $this->year); |
| 599 |
$d -= $days; |
| 600 |
|
| 601 |
list($this->year, $this->month, $this->day) = |
| 602 |
sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s"); |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Compares two dates |
| 607 |
* |
| 608 |
* Compares two dates. Suitable for use |
| 609 |
* in sorting functions. |
| 610 |
* |
| 611 |
* @access public |
| 612 |
* @param object Date $d1 the first date |
| 613 |
* @param object Date $d2 the second date |
| 614 |
* @return int 0 if the dates are equal, -1 if d1 is before d2, 1 if d1 is after d2 |
| 615 |
*/ |
| 616 |
function compare($d1, $d2) |
| 617 |
{ |
| 618 |
$d1->convertTZ(new Date_TimeZone('UTC')); |
| 619 |
$d2->convertTZ(new Date_TimeZone('UTC')); |
| 620 |
$days1 = Date_Calc::dateToDays($d1->day, $d1->month, $d1->year); |
| 621 |
$days2 = Date_Calc::dateToDays($d2->day, $d2->month, $d2->year); |
| 622 |
if($days1 < $days2) return -1; |
| 623 |
if($days1 > $days2) return 1; |
| 624 |
if($d1->hour < $d2->hour) return -1; |
| 625 |
if($d1->hour > $d2->hour) return 1; |
| 626 |
if($d1->minute < $d2->minute) return -1; |
| 627 |
if($d1->minute > $d2->minute) return 1; |
| 628 |
if($d1->second < $d2->second) return -1; |
| 629 |
if($d1->second > $d2->second) return 1; |
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* Test if this date/time is before a certian date/time |
| 635 |
* |
| 636 |
* Test if this date/time is before a certian date/time |
| 637 |
* |
| 638 |
* @access public |
| 639 |
* @param object Date $when the date to test against |
| 640 |
* @return boolean true if this date is before $when |
| 641 |
*/ |
| 642 |
function before($when) |
| 643 |
{ |
| 644 |
if(Date::compare($this,$when) == -1) return true; |
| 645 |
else return false; |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* Test if this date/time is after a certian date/time |
| 650 |
* |
| 651 |
* Test if this date/time is after a certian date/time |
| 652 |
* |
| 653 |
* @access public |
| 654 |
* @param object Date $when the date to test against |
| 655 |
* @return boolean true if this date is after $when |
| 656 |
*/ |
| 657 |
function after($when) |
| 658 |
{ |
| 659 |
if(Date::compare($this,$when) == 1) return true; |
| 660 |
else return false; |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Test if this date/time is exactly equal to a certian date/time |
| 665 |
* |
| 666 |
* Test if this date/time is exactly equal to a certian date/time |
| 667 |
* |
| 668 |
* @access public |
| 669 |
* @param object Date $when the date to test against |
| 670 |
* @return boolean true if this date is exactly equal to $when |
| 671 |
*/ |
| 672 |
function equals($when) |
| 673 |
{ |
| 674 |
if(Date::compare($this,$when) == 0) return true; |
| 675 |
else return false; |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Determine if this date is in the future |
| 680 |
* |
| 681 |
* Determine if this date is in the future |
| 682 |
* |
| 683 |
* @access public |
| 684 |
* @return boolean true if this date is in the future |
| 685 |
*/ |
| 686 |
function isFuture() |
| 687 |
{ |
| 688 |
$now = new Date(); |
| 689 |
if($this->after($now)) return true; |
| 690 |
else return false; |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Determine if this date is in the past |
| 695 |
* |
| 696 |
* Determine if this date is in the past |
| 697 |
* |
| 698 |
* @access public |
| 699 |
* @return boolean true if this date is in the past |
| 700 |
*/ |
| 701 |
function isPast() |
| 702 |
{ |
| 703 |
$now = new Date(); |
| 704 |
if($this->before($now)) return true; |
| 705 |
else return false; |
| 706 |
} |
| 707 |
|
| 708 |
/** |
| 709 |
* Determine if the year in this date is a leap year |
| 710 |
* |
| 711 |
* Determine if the year in this date is a leap year |
| 712 |
* |
| 713 |
* @access public |
| 714 |
* @return boolean true if this year is a leap year |
| 715 |
*/ |
| 716 |
function isLeapYear() |
| 717 |
{ |
| 718 |
return Date_Calc::isLeapYear($this->year); |
| 719 |
} |
| 720 |
|
| 721 |
/** |
| 722 |
* Get the Julian date for this date |
| 723 |
* |
| 724 |
* Get the Julian date for this date |
| 725 |
* |
| 726 |
* @access public |
| 727 |
* @return int the Julian date |
| 728 |
*/ |
| 729 |
function getJulianDate() |
| 730 |
{ |
| 731 |
return Date_Calc::julianDate($this->day, $this->month, $this->year); |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Gets the day of the week for this date |
| 736 |
* |
| 737 |
* Gets the day of the week for this date (0=Sunday) |
| 738 |
* |
| 739 |
* @access public |
| 740 |
* @return int the day of the week (0=Sunday) |
| 741 |
*/ |
| 742 |
function getDayOfWeek() |
| 743 |
{ |
| 744 |
return Date_Calc::dayOfWeek($this->day, $this->month, $this->year); |
| 745 |
} |
| 746 |
|
| 747 |
/** |
| 748 |
* Gets the week of the year for this date |
| 749 |
* |
| 750 |
* Gets the week of the year for this date |
| 751 |
* |
| 752 |
* @access public |
| 753 |
* @return int the week of the year |
| 754 |
*/ |
| 755 |
function getWeekOfYear() |
| 756 |
{ |
| 757 |
return Date_Calc::weekOfYear($this->day, $this->month, $this->year); |
| 758 |
} |
| 759 |
|
| 760 |
/** |
| 761 |
* Gets the quarter of the year for this date |
| 762 |
* |
| 763 |
* Gets the quarter of the year for this date |
| 764 |
* |
| 765 |
* @access public |
| 766 |
* @return int the quarter of the year (1-4) |
| 767 |
*/ |
| 768 |
function getQuarterOfYear() |
| 769 |
{ |
| 770 |
return Date_Calc::quarterOfYear($this->day, $this->month, $this->year); |
| 771 |
} |
| 772 |
|
| 773 |
/** |
| 774 |
* Gets number of days in the month for this date |
| 775 |
* |
| 776 |
* Gets number of days in the month for this date |
| 777 |
* |
| 778 |
* @access public |
| 779 |
* @return int number of days in this month |
| 780 |
*/ |
| 781 |
function getDaysInMonth() |
| 782 |
{ |
| 783 |
return Date_Calc::daysInMonth($this->month, $this->year); |
| 784 |
} |
| 785 |
|
| 786 |
/** |
| 787 |
* Gets the number of weeks in the month for this date |
| 788 |
* |
| 789 |
* Gets the number of weeks in the month for this date |
| 790 |
* |
| 791 |
* @access public |
| 792 |
* @return int number of weeks in this month |
| 793 |
*/ |
| 794 |
function getWeeksInMonth() |
| 795 |
{ |
| 796 |
return Date_Calc::weeksInMonth($this->month, $this->year); |
| 797 |
} |
| 798 |
|
| 799 |
/** |
| 800 |
* Gets the full name or abbriviated name of this weekday |
| 801 |
* |
| 802 |
* Gets the full name or abbriviated name of this weekday |
| 803 |
* |
| 804 |
* @access public |
| 805 |
* @param boolean $abbr abbrivate the name |
| 806 |
* @return string name of this day |
| 807 |
*/ |
| 808 |
function getDayName($abbr = false) |
| 809 |
{ |
| 810 |
if($abbr) |
| 811 |
return Date_Calc::getWeekdayAbbrname($this->day, $this->month, $this->year); |
| 812 |
else |
| 813 |
return Date_Calc::getWeekdayFullname($this->day, $this->month, $this->year); |
| 814 |
} |
| 815 |
|
| 816 |
/** |
| 817 |
* Gets the full name or abbriviated name of this month |
| 818 |
* |
| 819 |
* Gets the full name or abbriviated name of this month |
| 820 |
* |
| 821 |
* @access public |
| 822 |
* @param boolean $abbr abbrivate the name |
| 823 |
* @return string name of this month |
| 824 |
*/ |
| 825 |
function getMonthName($abbr = false) |
| 826 |
{ |
| 827 |
if($abbr) |
| 828 |
return Date_Calc::getMonthAbbrname($this->month); |
| 829 |
else |
| 830 |
return Date_Calc::getMonthFullname($this->month); |
| 831 |
} |
| 832 |
|
| 833 |
/** |
| 834 |
* Get a Date object for the day after this one |
| 835 |
* |
| 836 |
* Get a Date object for the day after this one. |
| 837 |
* The time of the returned Date object is the same as this time. |
| 838 |
* |
| 839 |
* @access public |
| 840 |
* @return object Date Date representing the next day |
| 841 |
*/ |
| 842 |
function getNextDay() |
| 843 |
{ |
| 844 |
$day = Date_Calc::nextDay($this->day, $this->month, $this->year, "%Y-%m-%d"); |
| 845 |
$date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second); |
| 846 |
$newDate = new Date(); |
| 847 |
$newDate->setDate($date); |
| 848 |
return $newDate; |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* Get a Date object for the day before this one |
| 853 |
* |
| 854 |
* Get a Date object for the day before this one. |
| 855 |
* The time of the returned Date object is the same as this time. |
| 856 |
* |
| 857 |
* @access public |
| 858 |
* @return object Date Date representing the previous day |
| 859 |
*/ |
| 860 |
function getPrevDay() |
| 861 |
{ |
| 862 |
$day = Date_Calc::prevDay($this->day, $this->month, $this->year, "%Y-%m-%d"); |
| 863 |
$date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second); |
| 864 |
$newDate = new Date(); |
| 865 |
$newDate->setDate($date); |
| 866 |
return $newDate; |
| 867 |
} |
| 868 |
|
| 869 |
/** |
| 870 |
* Get a Date object for the weekday after this one |
| 871 |
* |
| 872 |
* Get a Date object for the weekday after this one. |
| 873 |
* The time of the returned Date object is the same as this time. |
| 874 |
* |
| 875 |
* @access public |
| 876 |
* @return object Date Date representing the next weekday |
| 877 |
*/ |
| 878 |
function getNextWeekday() |
| 879 |
{ |
| 880 |
$day = Date_Calc::nextWeekday($this->day, $this->month, $this->year, "%Y-%m-%d"); |
| 881 |
$date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second); |
| 882 |
$newDate = new Date(); |
| 883 |
$newDate->setDate($date); |
| 884 |
return $newDate; |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* Get a Date object for the weekday before this one |
| 889 |
* |
| 890 |
* Get a Date object for the weekday before this one. |
| 891 |
* The time of the returned Date object is the same as this time. |
| 892 |
* |
| 893 |
* @access public |
| 894 |
* @return object Date Date representing the previous weekday |
| 895 |
*/ |
| 896 |
function getPrevWeekday() |
| 897 |
{ |
| 898 |
$day = Date_Calc::prevWeekday($this->day, $this->month, $this->year, "%Y-%m-%d"); |
| 899 |
$date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second); |
| 900 |
$newDate = new Date(); |
| 901 |
$newDate->setDate($date); |
| 902 |
return $newDate; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
/** |
| 907 |
* Returns the year field of the date object |
| 908 |
* |
| 909 |
* Returns the year field of the date object |
| 910 |
* |
| 911 |
* @access public |
| 912 |
* @return int the year |
| 913 |
*/ |
| 914 |
function getYear() |
| 915 |
{ |
| 916 |
return $this->year; |
| 917 |
} |
| 918 |
|
| 919 |
/** |
| 920 |
* Returns the month field of the date object |
| 921 |
* |
| 922 |
* Returns the month field of the date object |
| 923 |
* |
| 924 |
* @access public |
| 925 |
* @return int the month |
| 926 |
*/ |
| 927 |
function getMonth() |
| 928 |
{ |
| 929 |
return $this->month; |
| 930 |
} |
| 931 |
|
| 932 |
/** |
| 933 |
* Returns the day field of the date object |
| 934 |
* |
| 935 |
* Returns the day field of the date object |
| 936 |
* |
| 937 |
* @access public |
| 938 |
* @return int the day |
| 939 |
*/ |
| 940 |
function getDay() |
| 941 |
{ |
| 942 |
return $this->day; |
| 943 |
} |
| 944 |
|
| 945 |
/** |
| 946 |
* Returns the hour field of the date object |
| 947 |
* |
| 948 |
* Returns the hour field of the date object |
| 949 |
* |
| 950 |
* @access public |
| 951 |
* @return int the hour |
| 952 |
*/ |
| 953 |
function getHour() |
| 954 |
{ |
| 955 |
return $this->hour; |
| 956 |
} |
| 957 |
|
| 958 |
/** |
| 959 |
* Returns the minute field of the date object |
| 960 |
* |
| 961 |
* Returns the minute field of the date object |
| 962 |
* |
| 963 |
* @access public |
| 964 |
* @return int the minute |
| 965 |
*/ |
| 966 |
function getMinute() |
| 967 |
{ |
| 968 |
return $this->minute; |
| 969 |
} |
| 970 |
|
| 971 |
/** |
| 972 |
* Returns the second field of the date object |
| 973 |
* |
| 974 |
* Returns the second field of the date object |
| 975 |
* |
| 976 |
* @access public |
| 977 |
* @return int the second |
| 978 |
*/ |
| 979 |
function getSecond() |
| 980 |
{ |
| 981 |
return $this->second; |
| 982 |
} |
| 983 |
|
| 984 |
/** |
| 985 |
* Set the year field of the date object |
| 986 |
* |
| 987 |
* Set the year field of the date object, invalid years (not 0-9999) are set to 0. |
| 988 |
* |
| 989 |
* @access public |
| 990 |
* @param int $y the year |
| 991 |
*/ |
| 992 |
function setYear($y) |
| 993 |
{ |
| 994 |
if($y < 0 || $y > 9999) { |
| 995 |
$this->year = 0; |
| 996 |
} else { |
| 997 |
$this->year = $y; |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
/** |
| 1002 |
* Set the month field of the date object |
| 1003 |
* |
| 1004 |
* Set the month field of the date object, invalid months (not 1-12) are set to 1. |
| 1005 |
* |
| 1006 |
* @access public |
| 1007 |
* @param int $m the month |
| 1008 |
*/ |
| 1009 |
function setMonth($m) |
| 1010 |
{ |
| 1011 |
if($m < 1 || $m > 12) { |
| 1012 |
$this->month = 1; |
| 1013 |
} else { |
| 1014 |
$this->month = $m; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
/** |
| 1019 |
* Set the day field of the date object |
| 1020 |
* |
| 1021 |
* Set the day field of the date object, invalid days (not 1-31) are set to 1. |
| 1022 |
* |
| 1023 |
* @access public |
| 1024 |
* @param int $d the day |
| 1025 |
*/ |
| 1026 |
function setDay($d) |
| 1027 |
{ |
| 1028 |
if($d > 31 || $d < 1) { |
| 1029 |
$this->day = 1; |
| 1030 |
} else { |
| 1031 |
$this->day = $d; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
/** |
| 1036 |
* Set the hour field of the date object |
| 1037 |
* |
| 1038 |
* Set the hour field of the date object in 24-hour format. |
| 1039 |
* Invalid hours (not 0-23) are set to 0. |
| 1040 |
* |
| 1041 |
* @access public |
| 1042 |
* @param int $h the hour |
| 1043 |
*/ |
| 1044 |
function setHour($h) |
| 1045 |
{ |
| 1046 |
if($h > 23 || $h < 0) { |
| 1047 |
$this->hour = 0; |
| 1048 |
} else { |
| 1049 |
$this->hour = $h; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
/** |
| 1054 |
* Set the minute field of the date object |
| 1055 |
* |
| 1056 |
* Set the minute field of the date object, invalid minutes (not 0-59) are set to 0. |
| 1057 |
* |
| 1058 |
* @access public |
| 1059 |
* @param int $m the minute |
| 1060 |
*/ |
| 1061 |
function setMinute($m) |
| 1062 |
{ |
| 1063 |
if($m > 59 || $m < 0) { |
| 1064 |
$this->minute = 0; |
| 1065 |
} else { |
| 1066 |
$this->minute = $m; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
/** |
| 1071 |
* Set the second field of the date object |
| 1072 |
* |
| 1073 |
* Set the second field of the date object, invalid seconds (not 0-59) are set to 0. |
| 1074 |
* |
| 1075 |
* @access public |
| 1076 |
* @param int $s the second |
| 1077 |
*/ |
| 1078 |
function setSecond($s) { |
| 1079 |
if($s > 59 || $s < 0) { |
| 1080 |
$this->second = 0; |
| 1081 |
} else { |
| 1082 |
$this->second = $s; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
} // Date |
| 1087 |
|
| 1088 |
|
| 1089 |
// |
| 1090 |
// END |
| 1091 |
?> |