PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/content/code/trunk/administrator/components/com_artofcontent/libraries/joomla/utilities/date16.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 502 lines | 253 code | 54 blank | 195 comment | 22 complexity | fdd27809b6b5b407a1b7460617c43114 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: date16.php 484 2010-12-20 23:40:27Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage Utilities
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * JDate is a class that stores a date and provides logic to manipulate
  13. * and render that date in a variety of formats.
  14. *
  15. * @package Joomla.Framework
  16. * @subpackage Utilities
  17. * @since 1.5
  18. */
  19. class JDate16 extends DateTime
  20. {
  21. const DAY_ABBR = "\x021\x03";
  22. const DAY_NAME = "\x022\x03";
  23. const MONTH_ABBR = "\x023\x03";
  24. const MONTH_NAME = "\x024\x03";
  25. /**
  26. * The format string to be applied when using the __toString() magic method.
  27. *
  28. * @var string
  29. * @since 1.6
  30. */
  31. public static $format = 'Y-m-d H:i:s';
  32. /**
  33. * Placeholder for a DateTimeZone object with GMT as the time zone.
  34. *
  35. * @var object
  36. * @since 1.6
  37. */
  38. protected static $gmt;
  39. /**
  40. * Placeholder for a DateTimeZone object with the default server
  41. * time zone as the time zone.
  42. *
  43. * @var object
  44. * @since 1.6
  45. */
  46. protected static $stz;
  47. /**
  48. * An array of offsets and time zone strings representing the available
  49. * options from Joomla! 1.5 and below.
  50. *
  51. * @deprecated Deprecated since 1.6
  52. *
  53. * @var array
  54. * @since 1.6
  55. */
  56. protected static $offsets = array(
  57. '-12' => 'Etc/GMT-12',
  58. '-11' => 'Pacific/Midway',
  59. '-10' => 'Pacific/Honolulu',
  60. '-9.5' => 'Pacific/Marquesas',
  61. '-9' => 'US/Alaska',
  62. '-8' => 'US/Pacific',
  63. '-7' => 'US/Mountain',
  64. '-6' => 'US/Central',
  65. '-5' => 'US/Eastern',
  66. '-4.5' => 'America/Caracas',
  67. '-4' => 'America/Barbados',
  68. '-3.5' => 'Canada/Newfoundland',
  69. '-3' => 'America/Buenos_Aires',
  70. '-2' => 'Atlantic/South_Georgia',
  71. '-1' => 'Atlantic/Azores',
  72. '0' => 'Europe/London',
  73. '1' => 'Europe/Amsterdam',
  74. '2' => 'Europe/Istanbul',
  75. '3' => 'Asia/Riyadh',
  76. '3.5' => 'Asia/Tehran',
  77. '4' => 'Asia/Muscat',
  78. '4.5' => 'Asia/Kabul',
  79. '5' => 'Asia/Karachi',
  80. '5.5' => 'Asia/Calcutta',
  81. '5.75' => 'Asia/Katmandu',
  82. '6' => 'Asia/Dhaka',
  83. '6.5' => 'Indian/Cocos',
  84. '7' => 'Asia/Bangkok',
  85. '8' => 'Australia/Perth',
  86. '8.75' => 'Australia/West',
  87. '9' => 'Asia/Tokyo',
  88. '9.5' => 'Australia/Adelaide',
  89. '10' => 'Australia/Brisbane',
  90. '10.5' => 'Australia/Lord_Howe',
  91. '11' => 'Pacific/Kosrae',
  92. '11.5' => 'Pacific/Norfolk',
  93. '12' => 'Pacific/Auckland',
  94. '12.75' => 'Pacific/Chatham',
  95. '13' => 'Pacific/Tongatapu',
  96. '14' => 'Pacific/Kiritimati'
  97. );
  98. /**
  99. * The DateTimeZone object for usage in rending dates as strings.
  100. *
  101. * @var object
  102. * @since 1.6
  103. */
  104. protected $_tz;
  105. /**
  106. * Constructor.
  107. *
  108. * @param string String in a format accepted by strtotime(), defaults to "now".
  109. * @param mixed Time zone to be used for the date.
  110. * @return void
  111. * @since 1.5
  112. *
  113. * @throws JException
  114. */
  115. public function __construct($date = 'now', $tz = null)
  116. {
  117. // Create the base GMT and server time zone objects.
  118. if (empty(self::$gmt) || empty(self::$stz)) {
  119. self::$gmt = new DateTimeZone('GMT');
  120. self::$stz = new DateTimeZone(@date_default_timezone_get());
  121. }
  122. // If the time zone object is not set, attempt to build it.
  123. if (!($tz instanceof DateTimeZone)) {
  124. if ($tz === null) {
  125. $tz = self::$gmt;
  126. } elseif (is_numeric($tz)) {
  127. // Translate from offset.
  128. $tz = new DateTimeZone(self::$offsets[(string) $tz]);
  129. }
  130. elseif (is_string($tz)) {
  131. $tz = new DateTimeZone($tz);
  132. }
  133. }
  134. // If the date is numeric assume a unix timestamp and convert it.
  135. date_default_timezone_set('UTC');
  136. $date = is_numeric($date) ? date('c', $date) : $date;
  137. // Call the DateTime constructor.
  138. parent::__construct($date, $tz);
  139. // Set the timezone object for access later.
  140. $this->_tz = $tz;
  141. }
  142. /**
  143. * Magic method to access properties of the date given by class to the format method.
  144. *
  145. * @param string The name of the property.
  146. * @return mixed A value if the property name is valid, null otherwise.
  147. * @since 1.6
  148. */
  149. public function __get($name)
  150. {
  151. $value = null;
  152. switch ($name) {
  153. case 'daysinmonth':
  154. $value = $this->format('t', true);
  155. break;
  156. case 'dayofweek':
  157. $value = $this->format('N', true);
  158. break;
  159. case 'dayofyear':
  160. $value = $this->format('z', true);
  161. break;
  162. case 'day':
  163. $value = $this->format('d', true);
  164. break;
  165. case 'hour':
  166. $value = $this->format('H', true);
  167. break;
  168. case 'isleapyear':
  169. $value = (boolean) $this->format('L', true);
  170. break;
  171. case 'hour':
  172. $value = $this->format('H', true);
  173. break;
  174. case 'minute':
  175. $value = $this->format('i', true);
  176. break;
  177. case 'month':
  178. $value = $this->format('m', true);
  179. break;
  180. case 'ordinal':
  181. $value = $this->format('S', true);
  182. break;
  183. case 'second':
  184. $value = $this->format('s', true);
  185. break;
  186. case 'week':
  187. $value = $this->format('W', true);
  188. break;
  189. case 'year':
  190. $value = $this->format('Y', true);
  191. break;
  192. default:
  193. $trace = debug_backtrace();
  194. trigger_error(
  195. 'Undefined property via __get(): ' . $name .
  196. ' in ' . $trace[0]['file'] .
  197. ' on line ' . $trace[0]['line'],
  198. E_USER_NOTICE
  199. );
  200. }
  201. return $value;
  202. }
  203. /**
  204. * Magic method to render the date object in the format specified in the public
  205. * static member JDate::$format.
  206. *
  207. * @return string The date as a formatted string.
  208. * @since 1.6
  209. */
  210. public function __toString()
  211. {
  212. return (string) parent::format(self::$format);
  213. }
  214. /**
  215. * Translates day of week number to a string.
  216. *
  217. * @param integer The numeric day of the week.
  218. * @param boolean Return the abreviated day string?
  219. * @return string The day of the week.
  220. * @since 1.5
  221. */
  222. protected function dayToString($day, $abbr = false)
  223. {
  224. switch ($day) {
  225. case 0: return $abbr ? JText::_('SUN') : JText::_('SUNDAY');
  226. case 1: return $abbr ? JText::_('MON') : JText::_('MONDAY');
  227. case 2: return $abbr ? JText::_('TUE') : JText::_('TUESDAY');
  228. case 3: return $abbr ? JText::_('WED') : JText::_('WEDNESDAY');
  229. case 4: return $abbr ? JText::_('THU') : JText::_('THURSDAY');
  230. case 5: return $abbr ? JText::_('FRI') : JText::_('FRIDAY');
  231. case 6: return $abbr ? JText::_('SAT') : JText::_('SATURDAY');
  232. }
  233. }
  234. /**
  235. * Gets the date as a formatted string in a local calendar.
  236. *
  237. * @param string The date format specification string (see {@link PHP_MANUAL#date})
  238. * @param boolean True to return the date string in the local time zone, false to return it in GMT.
  239. * @return string The date string in the specified format format.
  240. * @since 1.6
  241. */
  242. public function calendar($format, $local = false)
  243. {
  244. return $this->format($format, $local);
  245. }
  246. /**
  247. * Gets the date as a formatted string.
  248. *
  249. * @param string The date format specification string (see {@link PHP_MANUAL#date})
  250. * @param boolean True to return the date string in the local time zone, false to return it in GMT.
  251. * @return string The date string in the specified format format.
  252. * @since 1.6
  253. */
  254. public function format($format, $local = false)
  255. {
  256. // Do string replacements for date format options that can be translated.
  257. $format = preg_replace('/(^|[^\\\])D/', "\\1".self::DAY_ABBR, $format);
  258. $format = preg_replace('/(^|[^\\\])l/', "\\1".self::DAY_NAME, $format);
  259. $format = preg_replace('/(^|[^\\\])M/', "\\1".self::MONTH_ABBR, $format);
  260. $format = preg_replace('/(^|[^\\\])F/', "\\1".self::MONTH_NAME, $format);
  261. // If the returned time should not be local use GMT.
  262. if ($local == false) {
  263. parent::setTimezone(self::$gmt);
  264. }
  265. // Format the date.
  266. $return = parent::format($format);
  267. // Manually modify the month and day strings in the formated time.
  268. if (strpos($return, self::DAY_ABBR) !== false) {
  269. $return = str_replace(self::DAY_ABBR, $this->dayToString(parent::format('w'), true), $return);
  270. }
  271. if (strpos($return, self::DAY_NAME) !== false) {
  272. $return = str_replace(self::DAY_NAME, $this->dayToString(parent::format('w')), $return);
  273. }
  274. if (strpos($return, self::MONTH_ABBR) !== false) {
  275. $return = str_replace(self::MONTH_ABBR, $this->monthToString(parent::format('n'), true), $return);
  276. }
  277. if (strpos($return, self::MONTH_NAME) !== false) {
  278. $return = str_replace(self::MONTH_NAME, $this->monthToString(parent::format('n')), $return);
  279. }
  280. if ($local == false) {
  281. parent::setTimezone($this->_tz);
  282. }
  283. return $return;
  284. }
  285. /**
  286. * Get the time offset from GMT in hours or seconds.
  287. *
  288. * @param boolean True to return the value in hours.
  289. * @return float The time offset from GMT either in hours in seconds.
  290. * @since 1.6
  291. */
  292. public function getOffsetFromGMT($hours = false)
  293. {
  294. return (float) $hours ? ($this->_tz->getOffset($this) / 3600) : $this->_tz->getOffset($this);
  295. }
  296. /**
  297. * Translates month number to a string.
  298. *
  299. * @param integer The numeric month of the year.
  300. * @param boolean Return the abreviated month string?
  301. * @return string The month of the year.
  302. * @since 1.5
  303. */
  304. protected function monthToString($month, $abbr = false)
  305. {
  306. switch ($month) {
  307. case 1: return $abbr ? JText::_('JANUARY_SHORT') : JText::_('JANUARY');
  308. case 2: return $abbr ? JText::_('FEBRUARY_SHORT') : JText::_('FEBRUARY');
  309. case 3: return $abbr ? JText::_('MARCH_SHORT') : JText::_('MARCH');
  310. case 4: return $abbr ? JText::_('APRIL_SHORT') : JText::_('APRIL');
  311. case 5: return $abbr ? JText::_('MAY_SHORT') : JText::_('MAY');
  312. case 6: return $abbr ? JText::_('JUNE_SHORT') : JText::_('JUNE');
  313. case 7: return $abbr ? JText::_('JULY_SHORT') : JText::_('JULY');
  314. case 8: return $abbr ? JText::_('AUGUST_SHORT') : JText::_('AUGUST');
  315. case 9: return $abbr ? JText::_('SEPTEMBER_SHORT') : JText::_('SEPTEMBER');
  316. case 10: return $abbr ? JText::_('OCTOBER_SHORT') : JText::_('OCTOBER');
  317. case 11: return $abbr ? JText::_('NOVEMBER_SHORT') : JText::_('NOVEMBER');
  318. case 12: return $abbr ? JText::_('DECEMBER_SHORT') : JText::_('DECEMBER');
  319. }
  320. }
  321. /**
  322. * Set the date offset (in hours).
  323. *
  324. * @deprecated Deprecated since 1.6
  325. *
  326. * @param float The offset in hours.
  327. * @return boolean True on success.
  328. * @since 1.5
  329. */
  330. public function setOffset($offset)
  331. {
  332. // Only set the timezone if the offset exists.
  333. if (isset(self::$offsets[(string) $offset])) {
  334. $this->_tz = new DateTimeZone(self::$offsets[(string) $offset]);
  335. $this->setTimezone($this->_tz);
  336. return true;
  337. }
  338. return false;
  339. }
  340. /**
  341. * Method to wrap the setTimezone() function and set the internal
  342. * time zone object.
  343. *
  344. * @param object The new DateTimeZone object.
  345. * @return object The old DateTimeZone object.
  346. * @since 1.6
  347. */
  348. public function setTimezone($tz)
  349. {
  350. if (is_numeric($tz)) {
  351. // Translate from offset.
  352. $tz = new DateTimeZone(self::$offsets[(string) $tz]);
  353. }
  354. $this->_tz = $tz;
  355. return parent::setTimezone($tz);
  356. }
  357. /**
  358. * Gets the date in a specific format
  359. *
  360. * Returns a string formatted according to the given format. Month and weekday names and
  361. * other language dependent strings respect the current locale
  362. *
  363. * @deprecated Deprecated since 1.6, use JDate::format() instead.
  364. *
  365. * @param string The date format specification string (see {@link PHP_MANUAL#strftime})
  366. * @param boolean True to return the date string in the local time zone, false to return it in GMT.
  367. * @return string The date as a formatted string.
  368. * @since 1.5
  369. */
  370. public function toFormat($format = '%Y-%m-%d %H:%M:%S', $local = false)
  371. {
  372. // Set time zone to GMT as strftime formats according locale setting.
  373. date_default_timezone_set('GMT');
  374. // Generate the timestamp.
  375. $time = (int) parent::format('U');
  376. // If the returned time should be local add the GMT offset.
  377. if ($local) {
  378. $time += $this->getOffsetFromGMT();
  379. }
  380. // Manually modify the month and day strings in the format.
  381. if (strpos($format, '%a') !== false) {
  382. $format = str_replace('%a', $this->dayToString(date('w', $time), true), $format);
  383. }
  384. if (strpos($format, '%A') !== false) {
  385. $format = str_replace('%A', $this->dayToString(date('w', $time)), $format);
  386. }
  387. if (strpos($format, '%b') !== false) {
  388. $format = str_replace('%b', $this->monthToString(date('n', $time), true), $format);
  389. }
  390. if (strpos($format, '%B') !== false) {
  391. $format = str_replace('%B', $this->monthToString(date('n', $time)), $format);
  392. }
  393. // Generate the formatted string.
  394. $date = strftime($format, $time);
  395. return $date;
  396. }
  397. /**
  398. * Gets the date as an ISO 8601 string. IETF RFC 3339 defines the ISO 8601 format
  399. * and it can be found at the IETF Web site.
  400. *
  401. * @link http://www.ietf.org/rfc/rfc3339.txt
  402. *
  403. * @param boolean True to return the date string in the local time zone, false to return it in GMT.
  404. * @return string The date string in ISO 8601 format.
  405. * @since 1.5
  406. */
  407. public function toISO8601($local = false)
  408. {
  409. return $this->format(DateTime::RFC3339, $local);
  410. }
  411. /**
  412. * Gets the date as an MySQL datetime string.
  413. *
  414. * @link http://dev.mysql.com/doc/refman/5.0/en/datetime.html
  415. *
  416. * @param boolean True to return the date string in the local time zone, false to return it in GMT.
  417. * @return string The date string in MySQL datetime format.
  418. * @since 1.5
  419. */
  420. public function toMySQL($local = false)
  421. {
  422. return $this->format('Y-m-d H:i:s', $local);
  423. }
  424. /**
  425. * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition
  426. * can be found at the IETF Web site.
  427. *
  428. * @link http://www.ietf.org/rfc/rfc2822.txt
  429. *
  430. * @param boolean True to return the date string in the local time zone, false to return it in GMT.
  431. * @return string The date string in RFC 822 format.
  432. * @since 1.5
  433. */
  434. public function toRFC822($local = false)
  435. {
  436. return $this->format(DateTime::RFC2822, $local);
  437. }
  438. /**
  439. * Gets the date as UNIX time stamp.
  440. *
  441. * @return integer The date as a UNIX timestamp.
  442. * @since 1.5
  443. */
  444. public function toUnix()
  445. {
  446. return (int) parent::format('U');
  447. }
  448. }