PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/sgl/includes/qcubed/_core/framework/QDateTime.class.php

http://logisticsouth.googlecode.com/
PHP | 838 lines | 498 code | 96 blank | 244 comment | 97 complexity | e8a33aabdf6d4964b26d6fdfda3bbec5 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. // These Aid with the PHP 5.2 DateTime error handling
  3. class QDateTimeNullException extends QCallerException {}
  4. function QDateTimeErrorHandler() {}
  5. /**
  6. * QDateTime (Standard)
  7. * REQUIRES: PHP >= 5.2.0
  8. *
  9. * This DateTime class manages datetimes for the entire system. It basically
  10. * provides a nice wrapper around the PHP DateTime class, which is included with
  11. * all versions of PHP >= 5.2.0.
  12. *
  13. * For legacy PHP users (PHP < 5.2.0), please refer to QDateTime.legacy
  14. */
  15. class QDateTime extends DateTime {
  16. const Now = 'now';
  17. const FormatIso = 'YYYY-MM-DD hhhh:mm:ss';
  18. const FormatIsoCompressed = 'YYYYMMDDhhhhmmss';
  19. const FormatDisplayDate = 'DD/MM/YYYY';
  20. const FormatDisplayDateFull = 'DDD, MMMM D, YYYY';
  21. const FormatDisplayDateTime = 'MMM DD YYYY hh:mm zz';
  22. const FormatDisplayDateTimeFull = 'DDDD, MMMM D, YYYY, h:mm:ss zz';
  23. const FormatDisplayTime = 'hh:mm:ss zz';
  24. const FormatRfc822 = 'DDD, DD MMM YYYY hhhh:mm:ss ttt';
  25. const FormatSoap = 'YYYY-MM-DDThhhh:mm:ss';
  26. /**
  27. * The "Default" Display Format
  28. * @var string $DefaultFormat
  29. */
  30. public static $DefaultFormat = QDateTime::FormatDisplayDate;
  31. /**
  32. * The "Default" Display Format for Times
  33. * @var string $DefaultTimeFormat
  34. */
  35. public static $DefaultTimeFormat = QDateTime::FormatDisplayTime;
  36. /**
  37. * Returns a new QDateTime object that's set to "Now"
  38. * Set blnTimeValue to true (default) for a DateTime, and set blnTimeValue to false for just a Date
  39. *
  40. * @param boolean $blnTimeValue whether or not to include the time value
  41. * @return QDateTime the current date and/or time
  42. */
  43. public static function Now($blnTimeValue = true) {
  44. $dttToReturn = new QDateTime(QDateTime::Now);
  45. if (!$blnTimeValue) {
  46. $dttToReturn->blnTimeNull = true;
  47. $dttToReturn->ReinforceNullProperties();
  48. }
  49. return $dttToReturn;
  50. }
  51. protected $blnDateNull = true;
  52. protected $blnTimeNull = true;
  53. public static function NowToString($strFormat = null) {
  54. $dttNow = new QDateTime(QDateTime::Now);
  55. return $dttNow->qFormat($strFormat);
  56. }
  57. public function IsDateNull() {
  58. return $this->blnDateNull;
  59. }
  60. public function IsNull() {
  61. return ($this->blnDateNull && $this->blnTimeNull);
  62. }
  63. public function IsTimeNull() {
  64. return $this->blnTimeNull;
  65. }
  66. public function PhpDate($strFormat) {
  67. // This just makes a call to format
  68. return parent::format($strFormat);
  69. }
  70. public function GetSoapDateTimeArray($dttArray) {
  71. if (!$dttArray)
  72. return null;
  73. $strArrayToReturn = array();
  74. foreach ($dttArray as $dttItem)
  75. array_push($strArrayToReturn, $dttItem->qFormat(QDateTime::FormatSoap));
  76. return $strArrayToReturn;
  77. }
  78. /**
  79. * @param integer $intTimestamp
  80. * @param DateTimeZone $objTimeZone
  81. * @return QDateTime
  82. */
  83. public static function FromTimestamp($intTimestamp, DateTimeZone $objTimeZone = null) {
  84. return new QDateTime(date('Y-m-d H:i:s', $intTimestamp), $objTimeZone);
  85. }
  86. public function __construct($mixValue = null, DateTimeZone $objTimeZone = null) {
  87. // Cloning from another QDateTime object
  88. if ($mixValue instanceof QDateTime) {
  89. if ($objTimeZone)
  90. throw new QCallerException('QDateTime cloning cannot take in a DateTimeZone parameter');
  91. if ($mixValue->GetTimeZone()->GetName() == date_default_timezone_get())
  92. parent::__construct($mixValue->format('Y-m-d H:i:s'));
  93. else
  94. parent::__construct($mixValue->format(DateTime::ISO8601));
  95. $this->blnDateNull = $mixValue->IsDateNull();
  96. $this->blnTimeNull = $mixValue->IsTimeNull();
  97. // Subclassing from a PHP DateTime object
  98. } else if ($mixValue instanceof DateTime) {
  99. if ($objTimeZone)
  100. throw new QCallerException('QDateTime subclassing of a DateTime object cannot take in a DateTimeZone parameter');
  101. parent::__construct($mixValue->format(DateTime::ISO8601));
  102. // By definition, a DateTime object doesn't have anything nulled
  103. $this->blnDateNull = false;
  104. $this->blnTimeNull = false;
  105. // Using "Now" constant
  106. } else if (strtolower($mixValue) == QDateTime::Now) {
  107. if ($objTimeZone)
  108. parent::__construct('now', $objTimeZone);
  109. else
  110. parent::__construct('now');
  111. $this->blnDateNull = false;
  112. $this->blnTimeNull = false;
  113. // Null or No Value
  114. } else if (!$mixValue) {
  115. // Set to "null date"
  116. // And Do Nothing Else -- Default Values are already set to Nulled out
  117. if ($objTimeZone)
  118. parent::__construct('2000-01-01 00:00:00', $objTimeZone);
  119. else
  120. parent::__construct('2000-01-01 00:00:00');
  121. // Parse the Value string
  122. } else {
  123. $intTimestamp = null;
  124. $blnValid = false;
  125. QApplication::SetErrorHandler('QDateTimeErrorHandler');
  126. try {
  127. if ($objTimeZone)
  128. $blnValid = parent::__construct($mixValue, $objTimeZone);
  129. else
  130. $blnValid = parent::__construct($mixValue);
  131. } catch (Exception $objExc) {}
  132. if ($blnValid !== false)
  133. $intTimestamp = parent::format('U');
  134. QApplication::RestoreErrorHandler();
  135. // Valid Value String
  136. if ($intTimestamp) {
  137. // To deal with "Tues" and date skipping bug in PHP 5.2
  138. parent::__construct(date('Y-m-d H:i:s', parent::format('U')));
  139. // if $mixValue represent only time string, blnDateNull must be set to true
  140. $objDateTime = (object)date_parse($mixValue);
  141. $this->blnDateNull = ($objDateTime->year && $objDateTime->month && $objDateTime->day)? false : true;
  142. // Update Time Null Value if Time was Specified
  143. if (strpos($mixValue, ':') !== false)
  144. $this->blnTimeNull = false;
  145. // Timestamp-based Value string
  146. } else if (is_numeric($mixValue)) {
  147. if ($objTimeZone)
  148. parent::__construct(date('Y-m-d H:i:s', $mixValue), $objTimeZone);
  149. else
  150. parent::__construct(date('Y-m-d H:i:s', $mixValue));
  151. $this->blnTimeNull = false;
  152. $this->blnDateNull = false;
  153. // Null Date
  154. } else {
  155. // Set to "null date"
  156. // And Do Nothing Else -- Default Values are already set to Nulled out
  157. if ($objTimeZone)
  158. parent::__construct('2000-01-01 00:00:00', $objTimeZone);
  159. else
  160. parent::__construct('2000-01-01 00:00:00');
  161. }
  162. }
  163. }
  164. /* The Following Methods are in place because of a bug in PHP 5.2.0 */
  165. protected $strSerializedData;
  166. public function __sleep() {
  167. $this->strSerializedData = parent::format(DateTime::ISO8601);
  168. return array('blnDateNull', 'blnTimeNull', 'strSerializedData');
  169. }
  170. public function __wakeup() {
  171. parent::__construct($this->strSerializedData);
  172. }
  173. /**
  174. * @deprecated since PHP 5.3
  175. * DEPRECATED - DO NOT USE. For PHP 5.3 compatability, this method should not be called with parameters.
  176. * In previous versions of QCubed and QCodo, the way to format a date was to call
  177. * __toString(), passing the format as a parameter. PHP 5.3 no longer supports this construct.
  178. * To format a date, call $myDateTimeObject->qFormat($strParameters).
  179. *
  180. * For compatibility with old apps, this method is preserved - and passing parameters to it is
  181. * allowed, through a horrible hack. Please DO NOT use in applications that were written past the
  182. * release of PHP 5.3.
  183. */
  184. public function __toString() {
  185. $strArgumentArray = func_get_args();
  186. if (count($strArgumentArray) >= 1) {
  187. $strFormat = $strArgumentArray[0];
  188. } else {
  189. $strFormat = null;
  190. }
  191. return $this->qFormat($strFormat);
  192. }
  193. /**
  194. * Outputs the date as a string given the format strFormat. By default,
  195. * it will return as QDateTime::FormatDisplayDate "MMM DD YYYY", e.g. Mar 20 1977.
  196. *
  197. * Properties of strFormat are (using Sunday, March 2, 1977 at 1:15:35 pm
  198. * in the following examples):
  199. *
  200. * M - Month as an integer (e.g., 3)
  201. * MM - Month as an integer with leading zero (e.g., 03)
  202. * MMM - Month as three-letters (e.g., Mar)
  203. * MMMM - Month as full name (e.g., March)
  204. *
  205. * D - Day as an integer (e.g., 2)
  206. * DD - Day as an integer with leading zero (e.g., 02)
  207. * DDD - Day of week as three-letters (e.g., Wed)
  208. * DDDD - Day of week as full name (e.g., Wednesday)
  209. *
  210. * YY - Year as a two-digit integer (e.g., 77)
  211. * YYYY - Year as a four-digit integer (e.g., 1977)
  212. *
  213. * h - Hour as an integer in 12-hour format (e.g., 1)
  214. * hh - Hour as an integer in 12-hour format with leading zero (e.g., 01)
  215. * hhh - Hour as an integer in 24-hour format (e.g., 13)
  216. * hhhh - Hour as an integer in 24-hour format with leading zero (e.g., 13)
  217. *
  218. * mm - Minute as a two-digit integer
  219. *
  220. * ss - Second as a two-digit integer
  221. *
  222. * z - "pm" or "am"
  223. * zz - "PM" or "AM"
  224. * zzz - "p.m." or "a.m."
  225. * zzzz - "P.M." or "A.M."
  226. *
  227. * ttt - Timezone Abbreviation as a three-letter code (e.g. PDT, GMT)
  228. * tttt - Timezone Identifier (e.g. America/Los_Angeles)
  229. *
  230. * @param string $strFormat the format of the date
  231. * @return string the formatted date as a string
  232. */
  233. public function qFormat($strFormat = null) {
  234. $this->ReinforceNullProperties();
  235. if (is_null($strFormat))
  236. $strFormat = QDateTime::$DefaultFormat;
  237. /*
  238. (?(?=D)([D]+)|
  239. (?(?=M)([M]+)|
  240. (?(?=Y)([Y]+)|
  241. (?(?=h)([h]+)|
  242. (?(?=m)([m]+)|
  243. (?(?=s)([s]+)|
  244. (?(?=z)([z]+)|
  245. (?(?=t)([t]+)|
  246. ))))))))
  247. */
  248. // $strArray = preg_split('/([^D^M^Y^h^m^s^z^t])+/', $strFormat);
  249. preg_match_all('/(?(?=D)([D]+)|(?(?=M)([M]+)|(?(?=Y)([Y]+)|(?(?=h)([h]+)|(?(?=m)([m]+)|(?(?=s)([s]+)|(?(?=z)([z]+)|(?(?=t)([t]+)|))))))))/', $strFormat, $strArray);
  250. $strArray = $strArray[0];
  251. $strToReturn = '';
  252. $intStartPosition = 0;
  253. for ($intIndex = 0; $intIndex < count($strArray); $intIndex++) {
  254. $strToken = trim($strArray[$intIndex]);
  255. if ($strToken) {
  256. $intEndPosition = strpos($strFormat, $strArray[$intIndex], $intStartPosition);
  257. $strToReturn .= substr($strFormat, $intStartPosition, $intEndPosition - $intStartPosition);
  258. $intStartPosition = $intEndPosition + strlen($strArray[$intIndex]);
  259. switch ($strArray[$intIndex]) {
  260. case 'M':
  261. $strToReturn .= parent::format('n');
  262. break;
  263. case 'MM':
  264. $strToReturn .= parent::format('m');
  265. break;
  266. case 'MMM':
  267. $strToReturn .= parent::format('M');
  268. break;
  269. case 'MMMM':
  270. $strToReturn .= parent::format('F');
  271. break;
  272. case 'D':
  273. $strToReturn .= parent::format('j');
  274. break;
  275. case 'DD':
  276. $strToReturn .= parent::format('d');
  277. break;
  278. case 'DDD':
  279. $strToReturn .= parent::format('D');
  280. break;
  281. case 'DDDD':
  282. $strToReturn .= parent::format('l');
  283. break;
  284. case 'YY':
  285. $strToReturn .= parent::format('y');
  286. break;
  287. case 'YYYY':
  288. $strToReturn .= parent::format('Y');
  289. break;
  290. case 'h':
  291. $strToReturn .= parent::format('g');
  292. break;
  293. case 'hh':
  294. $strToReturn .= parent::format('h');
  295. break;
  296. case 'hhh':
  297. $strToReturn .= parent::format('G');
  298. break;
  299. case 'hhhh':
  300. $strToReturn .= parent::format('H');
  301. break;
  302. case 'mm':
  303. $strToReturn .= parent::format('i');
  304. break;
  305. case 'ss':
  306. $strToReturn .= parent::format('s');
  307. break;
  308. case 'z':
  309. $strToReturn .= parent::format('a');
  310. break;
  311. case 'zz':
  312. $strToReturn .= parent::format('A');
  313. break;
  314. case 'zzz':
  315. $strToReturn .= sprintf('%s.m.', substr(parent::format('a'), 0, 1));
  316. break;
  317. case 'zzzz':
  318. $strToReturn .= sprintf('%s.M.', substr(parent::format('A'), 0, 1));
  319. break;
  320. case 'ttt':
  321. $strToReturn .= parent::format('T');
  322. break;
  323. case 'tttt':
  324. $strToReturn .= parent::format('e');
  325. break;
  326. default:
  327. $strToReturn .= $strArray[$intIndex];
  328. }
  329. }
  330. }
  331. if ($intStartPosition < strlen($strFormat))
  332. $strToReturn .= substr($strFormat, $intStartPosition);
  333. return $strToReturn;
  334. }
  335. public function format($strFormat) {
  336. $this->ReinforceNullProperties();
  337. return parent::format($strFormat);
  338. }
  339. public function setTime($intHour, $intMinute = null, $intSecond = null) {
  340. // If HOUR or MINUTE is NULL...
  341. if (is_null($intHour) || is_null($intMinute)) {
  342. parent::setTime($intHour, $intMinute, $intSecond);
  343. $this->blnTimeNull = true;
  344. return $this;
  345. }
  346. $intHour = QType::Cast($intHour, QType::Integer);
  347. $intMinute = QType::Cast($intMinute, QType::Integer);
  348. $intSecond = QType::Cast($intSecond, QType::Integer);
  349. $this->blnTimeNull = false;
  350. parent::setTime($intHour, $intMinute, $intSecond);
  351. return $this;
  352. }
  353. public function setDate($intYear, $intMonth, $intDay) {
  354. $intYear = QType::Cast($intYear, QType::Integer);
  355. $intMonth = QType::Cast($intMonth, QType::Integer);
  356. $intDay = QType::Cast($intDay, QType::Integer);
  357. $this->blnDateNull = false;
  358. parent::setDate($intYear, $intMonth, $intDay);
  359. return $this;
  360. }
  361. protected function ReinforceNullProperties() {
  362. if ($this->blnDateNull)
  363. parent::setDate(2000, 1, 1);
  364. if ($this->blnTimeNull)
  365. parent::setTime(0, 0, 0);
  366. }
  367. /**
  368. * Converts the current QDateTime object to a different TimeZone.
  369. *
  370. * TimeZone should be passed in as a string-based identifier.
  371. *
  372. * Note that this is different than the built-in DateTime::SetTimezone() method which expicitly
  373. * takes in a DateTimeZone object. QDateTime::ConvertToTimezone allows you to specify any
  374. * string-based Timezone identifier. If none is specified and/or if the specified timezone
  375. * is not a valid identifier, it will simply remain unchanged as opposed to throwing an exeception
  376. * or error.
  377. *
  378. * @param string $strTimezoneIdentifier a string-based parameter specifying a timezone identifier (e.g. America/Los_Angeles)
  379. * @return void
  380. */
  381. public function ConvertToTimezone($strTimezoneIdentifier) {
  382. try {
  383. $dtzNewTimezone = new DateTimeZone($strTimezoneIdentifier);
  384. $this->SetTimezone($dtzNewTimezone);
  385. } catch (Exception $objExc) {}
  386. }
  387. public function IsEqualTo(QDateTime $dttCompare) {
  388. // All comparison operations MUST have operands with matching Date Nullstates
  389. if ($this->blnDateNull != $dttCompare->blnDateNull)
  390. return false;
  391. // If mismatched Time nullstates, then only compare the Date portions
  392. if ($this->blnTimeNull != $dttCompare->blnTimeNull) {
  393. // Let's "Null Out" the Time
  394. $dttThis = new QDateTime($this);
  395. $dttThat = new QDateTime($dttCompare);
  396. $dttThis->Hour = null;
  397. $dttThat->Hour = null;
  398. // Return the Result
  399. return ($dttThis->Timestamp == $dttThat->Timestamp);
  400. } else {
  401. // Return the Result for the both Date and Time components
  402. return ($this->Timestamp == $dttCompare->Timestamp);
  403. }
  404. }
  405. public function IsEarlierThan(QDateTime $dttCompare) {
  406. // All comparison operations MUST have operands with matching Date Nullstates
  407. if ($this->blnDateNull != $dttCompare->blnDateNull)
  408. return false;
  409. // If mismatched Time nullstates, then only compare the Date portions
  410. if ($this->blnTimeNull != $dttCompare->blnTimeNull) {
  411. // Let's "Null Out" the Time
  412. $dttThis = new QDateTime($this);
  413. $dttThat = new QDateTime($dttCompare);
  414. $dttThis->Hour = null;
  415. $dttThat->Hour = null;
  416. // Return the Result
  417. return ($dttThis->Timestamp < $dttThat->Timestamp);
  418. } else {
  419. // Return the Result for the both Date and Time components
  420. return ($this->Timestamp < $dttCompare->Timestamp);
  421. }
  422. }
  423. public function IsEarlierOrEqualTo(QDateTime $dttCompare) {
  424. // All comparison operations MUST have operands with matching Date Nullstates
  425. if ($this->blnDateNull != $dttCompare->blnDateNull)
  426. return false;
  427. // If mismatched Time nullstates, then only compare the Date portions
  428. if ($this->blnTimeNull != $dttCompare->blnTimeNull) {
  429. // Let's "Null Out" the Time
  430. $dttThis = new QDateTime($this);
  431. $dttThat = new QDateTime($dttCompare);
  432. $dttThis->Hour = null;
  433. $dttThat->Hour = null;
  434. // Return the Result
  435. return ($dttThis->Timestamp <= $dttThat->Timestamp);
  436. } else {
  437. // Return the Result for the both Date and Time components
  438. return ($this->Timestamp <= $dttCompare->Timestamp);
  439. }
  440. }
  441. public function IsLaterThan(QDateTime $dttCompare) {
  442. // All comparison operations MUST have operands with matching Date Nullstates
  443. if ($this->blnDateNull != $dttCompare->blnDateNull)
  444. return false;
  445. // If mismatched Time nullstates, then only compare the Date portions
  446. if ($this->blnTimeNull != $dttCompare->blnTimeNull) {
  447. // Let's "Null Out" the Time
  448. $dttThis = new QDateTime($this);
  449. $dttThat = new QDateTime($dttCompare);
  450. $dttThis->Hour = null;
  451. $dttThat->Hour = null;
  452. // Return the Result
  453. return ($dttThis->Timestamp > $dttThat->Timestamp);
  454. } else {
  455. // Return the Result for the both Date and Time components
  456. return ($this->Timestamp > $dttCompare->Timestamp);
  457. }
  458. }
  459. public function IsLaterOrEqualTo(QDateTime $dttCompare) {
  460. // All comparison operations MUST have operands with matching Date Nullstates
  461. if ($this->blnDateNull != $dttCompare->blnDateNull)
  462. return false;
  463. // If mismatched Time nullstates, then only compare the Date portions
  464. if ($this->blnTimeNull != $dttCompare->blnTimeNull) {
  465. // Let's "Null Out" the Time
  466. $dttThis = new QDateTime($this);
  467. $dttThat = new QDateTime($dttCompare);
  468. $dttThis->Hour = null;
  469. $dttThat->Hour = null;
  470. // Return the Result
  471. return ($dttThis->Timestamp >= $dttThat->Timestamp);
  472. } else {
  473. // Return the Result for the both Date and Time components
  474. return ($this->Timestamp >= $dttCompare->Timestamp);
  475. }
  476. }
  477. public function Difference(QDateTime $dttDateTime) {
  478. $intDifference = $this->Timestamp - $dttDateTime->Timestamp;
  479. return new QDateTimeSpan($intDifference);
  480. }
  481. public function Add($dtsSpan){
  482. if (!$dtsSpan instanceof QDateTimeSpan) {
  483. throw new QCallerException("Can only add QDateTimeSpan objects");
  484. }
  485. // Get this DateTime timestamp
  486. $intTimestamp = $this->Timestamp;
  487. // And add the Span Second count to it
  488. $this->Timestamp = $this->Timestamp + $dtsSpan->Seconds;
  489. return $this;
  490. }
  491. public function AddSeconds($intSeconds){
  492. $this->Second += $intSeconds;
  493. return $this;
  494. }
  495. public function AddMinutes($intMinutes){
  496. $this->Minute += $intMinutes;
  497. return $this;
  498. }
  499. public function AddHours($intHours){
  500. $this->Hour += $intHours;
  501. return $this;
  502. }
  503. public function AddDays($intDays){
  504. $this->Day += $intDays;
  505. return $this;
  506. }
  507. public function AddMonths($intMonths){
  508. $this->Month += $intMonths;
  509. return $this;
  510. }
  511. public function AddYears($intYears){
  512. $this->Year += $intYears;
  513. return $this;
  514. }
  515. public function Modify($mixValue) {
  516. parent::modify($mixValue);
  517. return $this;
  518. }
  519. public function __get($strName) {
  520. $this->ReinforceNullProperties();
  521. switch ($strName) {
  522. case 'Month':
  523. if ($this->blnDateNull)
  524. return null;
  525. else
  526. return (int) parent::format('m');
  527. case 'Day':
  528. if ($this->blnDateNull)
  529. return null;
  530. else
  531. return (int) parent::format('d');
  532. case 'Year':
  533. if ($this->blnDateNull)
  534. return null;
  535. else
  536. return (int) parent::format('Y');
  537. case 'Hour':
  538. if ($this->blnTimeNull)
  539. return null;
  540. else
  541. return (int) parent::format('H');
  542. case 'Minute':
  543. if ($this->blnTimeNull)
  544. return null;
  545. else
  546. return (int) parent::format('i');
  547. case 'Second':
  548. if ($this->blnTimeNull)
  549. return null;
  550. else
  551. return (int) parent::format('s');
  552. case 'Timestamp':
  553. // Until PHP fixes a bug where lowest int is int(-2147483648) but lowest float/double is (-2147529600)
  554. // We return as a "double"
  555. return (double) parent::format('U');
  556. case 'Age':
  557. // Figure out the Difference from "Now"
  558. $dtsFromCurrent = $this->Difference(QDateTime::Now());
  559. // It's in the future ('about 2 hours from now')
  560. if ($dtsFromCurrent->IsPositive())
  561. return $dtsFromCurrent->SimpleDisplay() . ' from now';
  562. // It's in the past ('about 5 hours ago')
  563. else if ($dtsFromCurrent->IsNegative()) {
  564. $dtsFromCurrent->Seconds = abs($dtsFromCurrent->Seconds);
  565. return $dtsFromCurrent->SimpleDisplay() . ' ago';
  566. // It's current
  567. } else
  568. return 'right now';
  569. default:
  570. throw new QUndefinedPropertyException('GET', 'QDateTime', $strName);
  571. }
  572. }
  573. public function __set($strName, $mixValue) {
  574. try {
  575. switch ($strName) {
  576. case 'Month':
  577. if ($this->blnDateNull && (!is_null($mixValue)))
  578. throw new QDateTimeNullException('Cannot set the Month property on a null date. Use SetDate().');
  579. if (is_null($mixValue)) {
  580. $this->blnDateNull = true;
  581. $this->ReinforceNullProperties();
  582. return null;
  583. }
  584. $mixValue = QType::Cast($mixValue, QType::Integer);
  585. parent::setDate(parent::format('Y'), $mixValue, parent::format('d'));
  586. return $mixValue;
  587. case 'Day':
  588. if ($this->blnDateNull && (!is_null($mixValue)))
  589. throw new QDateTimeNullException('Cannot set the Day property on a null date. Use SetDate().');
  590. if (is_null($mixValue)) {
  591. $this->blnDateNull = true;
  592. $this->ReinforceNullProperties();
  593. return null;
  594. }
  595. $mixValue = QType::Cast($mixValue, QType::Integer);
  596. parent::setDate(parent::format('Y'), parent::format('m'), $mixValue);
  597. return $mixValue;
  598. case 'Year':
  599. if ($this->blnDateNull && (!is_null($mixValue)))
  600. throw new QDateTimeNullException('Cannot set the Year property on a null date. Use SetDate().');
  601. if (is_null($mixValue)) {
  602. $this->blnDateNull = true;
  603. $this->ReinforceNullProperties();
  604. return null;
  605. }
  606. $mixValue = QType::Cast($mixValue, QType::Integer);
  607. parent::setDate($mixValue, parent::format('m'), parent::format('d'));
  608. return $mixValue;
  609. case 'Hour':
  610. if ($this->blnTimeNull && (!is_null($mixValue)))
  611. throw new QDateTimeNullException('Cannot set the Hour property on a null time. Use SetTime().');
  612. if (is_null($mixValue)) {
  613. $this->blnTimeNull = true;
  614. $this->ReinforceNullProperties();
  615. return null;
  616. }
  617. $mixValue = QType::Cast($mixValue, QType::Integer);
  618. parent::setTime($mixValue, parent::format('i'), parent::format('s'));
  619. return $mixValue;
  620. case 'Minute':
  621. if ($this->blnTimeNull && (!is_null($mixValue)))
  622. throw new QDateTimeNullException('Cannot set the Minute property on a null time. Use SetTime().');
  623. if (is_null($mixValue)) {
  624. $this->blnTimeNull = true;
  625. $this->ReinforceNullProperties();
  626. return null;
  627. }
  628. $mixValue = QType::Cast($mixValue, QType::Integer);
  629. parent::setTime(parent::format('H'), $mixValue, parent::format('s'));
  630. return $mixValue;
  631. case 'Second':
  632. if ($this->blnTimeNull && (!is_null($mixValue)))
  633. throw new QDateTimeNullException('Cannot set the Second property on a null time. Use SetTime().');
  634. if (is_null($mixValue)) {
  635. $this->blnTimeNull = true;
  636. $this->ReinforceNullProperties();
  637. return null;
  638. }
  639. $mixValue = QType::Cast($mixValue, QType::Integer);
  640. parent::setTime(parent::format('H'), parent::format('i'), $mixValue);
  641. return $mixValue;
  642. case 'Timestamp':
  643. $mixValue = QType::Cast($mixValue, QType::Integer);
  644. $this->blnDateNull = false;
  645. $this->blnTimeNull = false;
  646. $this->SetDate(date('Y', $mixValue), date('m', $mixValue), date('d', $mixValue));
  647. $this->SetTime(date('H', $mixValue), date('i', $mixValue), date('s', $mixValue));
  648. return $mixValue;
  649. default:
  650. throw new QUndefinedPropertyException('SET', 'QDateTime', $strName);
  651. }
  652. } catch (QInvalidCastException $objExc) {
  653. $objExc->IncrementOffset();
  654. throw $objExc;
  655. }
  656. }
  657. }
  658. /*
  659. This is a reference to the documentation for hte PHP DateTime classes (as of PHP 5.2)
  660. DateTime::ATOM
  661. DateTime::COOKIE
  662. DateTime::ISO8601
  663. DateTime::RFC822
  664. DateTime::RFC850
  665. DateTime::RFC1036
  666. DateTime::RFC1123
  667. DateTime::RFC2822
  668. DateTime::RFC3339
  669. DateTime::RSS
  670. DateTime::W3C
  671. DateTime::__construct([string time[, DateTimeZone object]])
  672. - Returns new DateTime object
  673. string DateTime::format(string format)
  674. - Returns date formatted according to given format
  675. long DateTime::getOffset()
  676. - Returns the DST offset
  677. DateTimeZone DateTime::getTimezone()
  678. - Return new DateTimeZone object relative to give DateTime
  679. void DateTime::modify(string modify)
  680. - Alters the timestamp
  681. array DateTime::parse(string date)
  682. - Returns associative array with detailed info about given date
  683. void DateTime::setDate(long year, long month, long day)
  684. - Sets the date
  685. void DateTime::setISODate(long year, long week[, long day])
  686. - Sets the ISO date
  687. void DateTime::setTime(long hour, long minute[, long second])
  688. - Sets the time
  689. void DateTime::setTimezone(DateTimeZone object)
  690. - Sets the timezone for the DateTime object
  691. */
  692. /* Some quick and dirty test harnesses
  693. $dtt1 = new QDateTime();
  694. $dtt2 = new QDateTime();
  695. printTable($dtt1, $dtt2);
  696. $dtt2->setDate(2000, 1, 1);
  697. $dtt1->setTime(0,0,3);
  698. $dtt2->setTime(0,0,2);
  699. // $dtt2->Month++;
  700. printTable($dtt1, $dtt2);
  701. function printTable($dtt1, $dtt2) {
  702. print('<table border="1" cellpadding="2"><tr><td>');
  703. printDate($dtt1);
  704. print('</td><td>');
  705. printDate($dtt2);
  706. print ('</td></tr>');
  707. print ('<tr><td colspan="2" align="center">IsEqualTo: <b>' . (($dtt1->IsEqualTo($dtt2)) ? 'Yes' : 'No') . '</b></td></tr>');
  708. print ('<tr><td colspan="2" align="center">IsEarlierThan: <b>' . (($dtt1->IsEarlierThan($dtt2)) ? 'Yes' : 'No') . '</b></td></tr>');
  709. print ('<tr><td colspan="2" align="center">IsLaterThan: <b>' . (($dtt1->IsLaterThan($dtt2)) ? 'Yes' : 'No') . '</b></td></tr>');
  710. print ('<tr><td colspan="2" align="center">IsEarlierOrEqualTo: <b>' . (($dtt1->IsEarlierOrEqualTo($dtt2)) ? 'Yes' : 'No') . '</b></td></tr>');
  711. print ('<tr><td colspan="2" align="center">IsLaterOrEqualTo: <b>' . (($dtt1->IsLaterOrEqualTo($dtt2)) ? 'Yes' : 'No') . '</b></td></tr>');
  712. print('</table>');
  713. }
  714. function printDate($dtt) {
  715. print ('Time Null: ' . (($dtt->IsTimeNull()) ? 'Yes' : 'No'));
  716. print ('<br/>');
  717. print ('Date Null: ' . (($dtt->IsDateNull()) ? 'Yes' : 'No'));
  718. print ('<br/>');
  719. print ('Date: ' . $dtt->qFormat(QDateTime::FormatDisplayDateTimeFull));
  720. print ('<br/>');
  721. print ('Month: ' . $dtt->Month . '<br/>');
  722. print ('Day: ' . $dtt->Day . '<br/>');
  723. print ('Year: ' . $dtt->Year . '<br/>');
  724. print ('Hour: ' . $dtt->Hour . '<br/>');
  725. print ('Minute: ' . $dtt->Minute . '<br/>');
  726. print ('Second: ' . $dtt->Second . '<br/>');
  727. }*/
  728. ?>