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

/libraries/joomla/date/date.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 462 lines | 217 code | 47 blank | 198 comment | 18 complexity | 20e1cfe8a0fff2a3acc836660ddcf958 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Date
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * JDate is a class that stores a date and provides logic to manipulate
  12. * and render that date in a variety of formats.
  13. *
  14. * @property-read string $daysinmonth t - Number of days in the given month.
  15. * @property-read string $dayofweek N - ISO-8601 numeric representation of the day of the week.
  16. * @property-read string $dayofyear z - The day of the year (starting from 0).
  17. * @property-read boolean $isleapyear L - Whether it's a leap year.
  18. * @property-read string $day d - Day of the month, 2 digits with leading zeros.
  19. * @property-read string $hour H - 24-hour format of an hour with leading zeros.
  20. * @property-read string $minute i - Minutes with leading zeros.
  21. * @property-read string $second s - Seconds with leading zeros.
  22. * @property-read string $month m - Numeric representation of a month, with leading zeros.
  23. * @property-read string $ordinal S - English ordinal suffix for the day of the month, 2 characters.
  24. * @property-read string $week W - Numeric representation of the day of the week.
  25. * @property-read string $year Y - A full numeric representation of a year, 4 digits.
  26. *
  27. * @package Joomla.Platform
  28. * @subpackage Date
  29. * @since 11.1
  30. */
  31. class JDate extends DateTime
  32. {
  33. const DAY_ABBR = "\x021\x03";
  34. const DAY_NAME = "\x022\x03";
  35. const MONTH_ABBR = "\x023\x03";
  36. const MONTH_NAME = "\x024\x03";
  37. /**
  38. * The format string to be applied when using the __toString() magic method.
  39. *
  40. * @var string
  41. * @since 11.1
  42. */
  43. public static $format = 'Y-m-d H:i:s';
  44. /**
  45. * Placeholder for a DateTimeZone object with GMT as the time zone.
  46. *
  47. * @var object
  48. * @since 11.1
  49. */
  50. protected static $gmt;
  51. /**
  52. * Placeholder for a DateTimeZone object with the default server
  53. * time zone as the time zone.
  54. *
  55. * @var object
  56. * @since 11.1
  57. */
  58. protected static $stz;
  59. /**
  60. * The DateTimeZone object for usage in rending dates as strings.
  61. *
  62. * @var DateTimeZone
  63. * @since 12.1
  64. */
  65. protected $tz;
  66. /**
  67. * Constructor.
  68. *
  69. * @param string $date String in a format accepted by strtotime(), defaults to "now".
  70. * @param mixed $tz Time zone to be used for the date. Might be a string or a DateTimeZone object.
  71. *
  72. * @since 11.1
  73. */
  74. public function __construct($date = 'now', $tz = null)
  75. {
  76. // Create the base GMT and server time zone objects.
  77. if (empty(self::$gmt) || empty(self::$stz))
  78. {
  79. self::$gmt = new DateTimeZone('GMT');
  80. self::$stz = new DateTimeZone(@date_default_timezone_get());
  81. }
  82. // If the time zone object is not set, attempt to build it.
  83. if (!($tz instanceof DateTimeZone))
  84. {
  85. if ($tz === null)
  86. {
  87. $tz = self::$gmt;
  88. }
  89. elseif (is_string($tz))
  90. {
  91. $tz = new DateTimeZone($tz);
  92. }
  93. }
  94. // If the date is numeric assume a unix timestamp and convert it.
  95. date_default_timezone_set('UTC');
  96. $date = is_numeric($date) ? date('c', $date) : $date;
  97. // Call the DateTime constructor.
  98. parent::__construct($date, $tz);
  99. // Reset the timezone for 3rd party libraries/extension that does not use JDate
  100. date_default_timezone_set(self::$stz->getName());
  101. // Set the timezone object for access later.
  102. $this->tz = $tz;
  103. }
  104. /**
  105. * Magic method to access properties of the date given by class to the format method.
  106. *
  107. * @param string $name The name of the property.
  108. *
  109. * @return mixed A value if the property name is valid, null otherwise.
  110. *
  111. * @since 11.1
  112. */
  113. public function __get($name)
  114. {
  115. $value = null;
  116. switch ($name)
  117. {
  118. case 'daysinmonth':
  119. $value = $this->format('t', true);
  120. break;
  121. case 'dayofweek':
  122. $value = $this->format('N', true);
  123. break;
  124. case 'dayofyear':
  125. $value = $this->format('z', true);
  126. break;
  127. case 'isleapyear':
  128. $value = (boolean) $this->format('L', true);
  129. break;
  130. case 'day':
  131. $value = $this->format('d', true);
  132. break;
  133. case 'hour':
  134. $value = $this->format('H', true);
  135. break;
  136. case 'minute':
  137. $value = $this->format('i', true);
  138. break;
  139. case 'second':
  140. $value = $this->format('s', true);
  141. break;
  142. case 'month':
  143. $value = $this->format('m', true);
  144. break;
  145. case 'ordinal':
  146. $value = $this->format('S', true);
  147. break;
  148. case 'week':
  149. $value = $this->format('W', true);
  150. break;
  151. case 'year':
  152. $value = $this->format('Y', true);
  153. break;
  154. default:
  155. $trace = debug_backtrace();
  156. trigger_error(
  157. 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'],
  158. E_USER_NOTICE
  159. );
  160. }
  161. return $value;
  162. }
  163. /**
  164. * Magic method to render the date object in the format specified in the public
  165. * static member JDate::$format.
  166. *
  167. * @return string The date as a formatted string.
  168. *
  169. * @since 11.1
  170. */
  171. public function __toString()
  172. {
  173. return (string) parent::format(self::$format);
  174. }
  175. /**
  176. * Proxy for new JDate().
  177. *
  178. * @param string $date String in a format accepted by strtotime(), defaults to "now".
  179. * @param mixed $tz Time zone to be used for the date.
  180. *
  181. * @return JDate
  182. *
  183. * @since 11.3
  184. */
  185. public static function getInstance($date = 'now', $tz = null)
  186. {
  187. return new JDate($date, $tz);
  188. }
  189. /**
  190. * Translates day of week number to a string.
  191. *
  192. * @param integer $day The numeric day of the week.
  193. * @param boolean $abbr Return the abbreviated day string?
  194. *
  195. * @return string The day of the week.
  196. *
  197. * @since 11.1
  198. */
  199. public function dayToString($day, $abbr = false)
  200. {
  201. switch ($day)
  202. {
  203. case 0:
  204. return $abbr ? JText::_('SUN') : JText::_('SUNDAY');
  205. case 1:
  206. return $abbr ? JText::_('MON') : JText::_('MONDAY');
  207. case 2:
  208. return $abbr ? JText::_('TUE') : JText::_('TUESDAY');
  209. case 3:
  210. return $abbr ? JText::_('WED') : JText::_('WEDNESDAY');
  211. case 4:
  212. return $abbr ? JText::_('THU') : JText::_('THURSDAY');
  213. case 5:
  214. return $abbr ? JText::_('FRI') : JText::_('FRIDAY');
  215. case 6:
  216. return $abbr ? JText::_('SAT') : JText::_('SATURDAY');
  217. }
  218. }
  219. /**
  220. * Gets the date as a formatted string in a local calendar.
  221. *
  222. * @param string $format The date format specification string (see {@link PHP_MANUAL#date})
  223. * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
  224. * @param boolean $translate True to translate localised strings
  225. *
  226. * @return string The date string in the specified format format.
  227. *
  228. * @since 11.1
  229. */
  230. public function calendar($format, $local = false, $translate = true)
  231. {
  232. return $this->format($format, $local, $translate);
  233. }
  234. /**
  235. * Gets the date as a formatted string.
  236. *
  237. * @param string $format The date format specification string (see {@link PHP_MANUAL#date})
  238. * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
  239. * @param boolean $translate True to translate localised strings
  240. *
  241. * @return string The date string in the specified format format.
  242. *
  243. * @since 11.1
  244. */
  245. public function format($format, $local = false, $translate = true)
  246. {
  247. if ($translate)
  248. {
  249. // Do string replacements for date format options that can be translated.
  250. $format = preg_replace('/(^|[^\\\])D/', "\\1" . self::DAY_ABBR, $format);
  251. $format = preg_replace('/(^|[^\\\])l/', "\\1" . self::DAY_NAME, $format);
  252. $format = preg_replace('/(^|[^\\\])M/', "\\1" . self::MONTH_ABBR, $format);
  253. $format = preg_replace('/(^|[^\\\])F/', "\\1" . self::MONTH_NAME, $format);
  254. }
  255. // If the returned time should not be local use GMT.
  256. if ($local == false)
  257. {
  258. parent::setTimezone(self::$gmt);
  259. }
  260. // Format the date.
  261. $return = parent::format($format);
  262. if ($translate)
  263. {
  264. // Manually modify the month and day strings in the formatted time.
  265. if (strpos($return, self::DAY_ABBR) !== false)
  266. {
  267. $return = str_replace(self::DAY_ABBR, $this->dayToString(parent::format('w'), true), $return);
  268. }
  269. if (strpos($return, self::DAY_NAME) !== false)
  270. {
  271. $return = str_replace(self::DAY_NAME, $this->dayToString(parent::format('w')), $return);
  272. }
  273. if (strpos($return, self::MONTH_ABBR) !== false)
  274. {
  275. $return = str_replace(self::MONTH_ABBR, $this->monthToString(parent::format('n'), true), $return);
  276. }
  277. if (strpos($return, self::MONTH_NAME) !== false)
  278. {
  279. $return = str_replace(self::MONTH_NAME, $this->monthToString(parent::format('n')), $return);
  280. }
  281. }
  282. if ($local == false)
  283. {
  284. parent::setTimezone($this->tz);
  285. }
  286. return $return;
  287. }
  288. /**
  289. * Get the time offset from GMT in hours or seconds.
  290. *
  291. * @param boolean $hours True to return the value in hours.
  292. *
  293. * @return float The time offset from GMT either in hours or in seconds.
  294. *
  295. * @since 11.1
  296. */
  297. public function getOffsetFromGMT($hours = false)
  298. {
  299. return (float) $hours ? ($this->tz->getOffset($this) / 3600) : $this->tz->getOffset($this);
  300. }
  301. /**
  302. * Translates month number to a string.
  303. *
  304. * @param integer $month The numeric month of the year.
  305. * @param boolean $abbr If true, return the abbreviated month string
  306. *
  307. * @return string The month of the year.
  308. *
  309. * @since 11.1
  310. */
  311. public function monthToString($month, $abbr = false)
  312. {
  313. switch ($month)
  314. {
  315. case 1:
  316. return $abbr ? JText::_('JANUARY_SHORT') : JText::_('JANUARY');
  317. case 2:
  318. return $abbr ? JText::_('FEBRUARY_SHORT') : JText::_('FEBRUARY');
  319. case 3:
  320. return $abbr ? JText::_('MARCH_SHORT') : JText::_('MARCH');
  321. case 4:
  322. return $abbr ? JText::_('APRIL_SHORT') : JText::_('APRIL');
  323. case 5:
  324. return $abbr ? JText::_('MAY_SHORT') : JText::_('MAY');
  325. case 6:
  326. return $abbr ? JText::_('JUNE_SHORT') : JText::_('JUNE');
  327. case 7:
  328. return $abbr ? JText::_('JULY_SHORT') : JText::_('JULY');
  329. case 8:
  330. return $abbr ? JText::_('AUGUST_SHORT') : JText::_('AUGUST');
  331. case 9:
  332. return $abbr ? JText::_('SEPTEMBER_SHORT') : JText::_('SEPTEMBER');
  333. case 10:
  334. return $abbr ? JText::_('OCTOBER_SHORT') : JText::_('OCTOBER');
  335. case 11:
  336. return $abbr ? JText::_('NOVEMBER_SHORT') : JText::_('NOVEMBER');
  337. case 12:
  338. return $abbr ? JText::_('DECEMBER_SHORT') : JText::_('DECEMBER');
  339. }
  340. }
  341. /**
  342. * Method to wrap the setTimezone() function and set the internal time zone object.
  343. *
  344. * @param DateTimeZone $tz The new DateTimeZone object.
  345. *
  346. * @return JDate
  347. *
  348. * @since 11.1
  349. */
  350. public function setTimezone($tz)
  351. {
  352. $this->tz = $tz;
  353. return parent::setTimezone($tz);
  354. }
  355. /**
  356. * Gets the date as an ISO 8601 string. IETF RFC 3339 defines the ISO 8601 format
  357. * and it can be found at the IETF Web site.
  358. *
  359. * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
  360. *
  361. * @return string The date string in ISO 8601 format.
  362. *
  363. * @link http://www.ietf.org/rfc/rfc3339.txt
  364. * @since 11.1
  365. */
  366. public function toISO8601($local = false)
  367. {
  368. return $this->format(DateTime::RFC3339, $local, false);
  369. }
  370. /**
  371. * Gets the date as an SQL datetime string.
  372. *
  373. * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
  374. * @param JDatabaseDriver $db The database driver or null to use JFactory::getDbo()
  375. *
  376. * @return string The date string in SQL datetime format.
  377. *
  378. * @link http://dev.mysql.com/doc/refman/5.0/en/datetime.html
  379. * @since 11.4
  380. */
  381. public function toSql($local = false, JDatabaseDriver $db = null)
  382. {
  383. if ($db === null)
  384. {
  385. $db = JFactory::getDbo();
  386. }
  387. return $this->format($db->getDateFormat(), $local, false);
  388. }
  389. /**
  390. * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition
  391. * can be found at the IETF Web site.
  392. *
  393. * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
  394. *
  395. * @return string The date string in RFC 822 format.
  396. *
  397. * @link http://www.ietf.org/rfc/rfc2822.txt
  398. * @since 11.1
  399. */
  400. public function toRFC822($local = false)
  401. {
  402. return $this->format(DateTime::RFC2822, $local, false);
  403. }
  404. /**
  405. * Gets the date as UNIX time stamp.
  406. *
  407. * @return integer The date as a UNIX timestamp.
  408. *
  409. * @since 11.1
  410. */
  411. public function toUnix()
  412. {
  413. return (int) parent::format('U');
  414. }
  415. }