Date.php 37 KB

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