PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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