PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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