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

/includes/qcodo/_core/framework/QDateTime.class.php

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