PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/utilities/date.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 303 lines | 173 code | 19 blank | 111 comment | 37 complexity | 764ac9594fdaec8620337d6beda03301 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: date.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Utilities
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * JDate is a class that stores a date
  18. *
  19. * @package Joomla.Framework
  20. * @subpackage Utilities
  21. * @since 1.5
  22. */
  23. class JDate extends JObject
  24. {
  25. /**
  26. * Unix timestamp
  27. *
  28. * @var int|boolean
  29. * @access protected
  30. */
  31. var $_date = false;
  32. /**
  33. * Time offset (in seconds)
  34. *
  35. * @var string
  36. * @access protected
  37. */
  38. var $_offset = 0;
  39. /**
  40. * Creates a new instance of JDate representing a given date.
  41. *
  42. * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  43. * If not specified, the current date and time is used.
  44. *
  45. * @param mixed $date optional the date this JDate will represent.
  46. * @param int $tzOffset optional the timezone $date is from
  47. */
  48. function __construct($date = 'now', $tzOffset = 0)
  49. {
  50. if ($date == 'now' || empty($date))
  51. {
  52. $this->_date = strtotime(gmdate("M d Y H:i:s", time()));
  53. return;
  54. }
  55. $tzOffset *= 3600;
  56. if (is_numeric($date))
  57. {
  58. $this->_date = $date - $tzOffset;
  59. return;
  60. }
  61. if (preg_match('~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~i',$date,$matches))
  62. {
  63. $months = Array(
  64. 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
  65. 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
  66. 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12
  67. );
  68. $matches[2] = strtolower($matches[2]);
  69. if (! isset($months[$matches[2]])) {
  70. return;
  71. }
  72. $this->_date = mktime(
  73. $matches[4], $matches[5], $matches[6],
  74. $months[$matches[2]], $matches[1], $matches[3]
  75. );
  76. if ($this->_date === false) {
  77. return;
  78. }
  79. if ($matches[7][0] == '+') {
  80. $tzOffset = 3600 * substr($matches[7], 1, 2)
  81. + 60 * substr($matches[7], -2);
  82. } elseif ($matches[7][0] == '-') {
  83. $tzOffset = -3600 * substr($matches[7], 1, 2)
  84. - 60 * substr($matches[7], -2);
  85. } else {
  86. if (strlen($matches[7]) == 1) {
  87. $oneHour = 3600;
  88. $ord = ord($matches[7]);
  89. if ($ord < ord('M')) {
  90. $tzOffset = (ord('A') - $ord - 1) * $oneHour;
  91. } elseif ($ord >= ord('M') && $matches[7] != 'Z') {
  92. $tzOffset = ($ord - ord('M')) * $oneHour;
  93. } elseif ($matches[7] == 'Z') {
  94. $tzOffset = 0;
  95. }
  96. }
  97. switch ($matches[7]) {
  98. case 'UT':
  99. case 'GMT': $tzOffset = 0;
  100. }
  101. }
  102. $this->_date -= $tzOffset;
  103. return;
  104. }
  105. if (preg_match('~(\\d{4})-(\\d{2})-(\\d{2})[T\s](\\d{2}):(\\d{2}):(\\d{2})(.*)~', $date, $matches))
  106. {
  107. $this->_date = mktime(
  108. $matches[4], $matches[5], $matches[6],
  109. $matches[2], $matches[3], $matches[1]
  110. );
  111. if ($this->_date == false) {
  112. return;
  113. }
  114. if (isset($matches[7][0])) {
  115. if ($matches[7][0] == '+' || $matches[7][0] == '-') {
  116. $tzOffset = 60 * (
  117. substr($matches[7], 0, 3) * 60 + substr($matches[7], -2)
  118. );
  119. } elseif ($matches[7] == 'Z') {
  120. $tzOffset = 0;
  121. }
  122. }
  123. $this->_date -= $tzOffset;
  124. return;
  125. }
  126. $this->_date = (strtotime($date) == -1) ? false : strtotime($date);
  127. if ($this->_date) {
  128. $this->_date -= $tzOffset;
  129. }
  130. }
  131. /**
  132. * Set the date offset (in hours)
  133. *
  134. * @access public
  135. * @param float The offset in hours
  136. */
  137. function setOffset($offset) {
  138. $this->_offset = 3600 * $offset;
  139. }
  140. /**
  141. * Get the date offset (in hours)
  142. *
  143. * @access public
  144. * @return integer
  145. */
  146. function getOffset() {
  147. return ((float) $this->_offset) / 3600.0;
  148. }
  149. /**
  150. * Gets the date as an RFC 822 date.
  151. *
  152. * @return a date in RFC 822 format
  153. * @link http://www.ietf.org/rfc/rfc2822.txt?number=2822 IETF RFC 2822
  154. * (replaces RFC 822)
  155. */
  156. function toRFC822($local = false)
  157. {
  158. $date = ($local) ? $this->_date + $this->_offset : $this->_date;
  159. $date = ($this->_date !== false) ? date('D, d M Y H:i:s', $date).' +0000' : null;
  160. return $date;
  161. }
  162. /**
  163. * Gets the date as an ISO 8601 date.
  164. *
  165. * @return a date in ISO 8601 (RFC 3339) format
  166. * @link http://www.ietf.org/rfc/rfc3339.txt?number=3339 IETF RFC 3339
  167. */
  168. function toISO8601($local = false)
  169. {
  170. $date = ($local) ? $this->_date + $this->_offset : $this->_date;
  171. $offset = $this->getOffset();
  172. $offset = ($local && $this->_offset) ? sprintf("%+03d:%02d", $offset, abs(($offset-intval($offset))*60) ) : 'Z';
  173. $date = ($this->_date !== false) ? date('Y-m-d\TH:i:s', $date).$offset : null;
  174. return $date;
  175. }
  176. /**
  177. * Gets the date as in MySQL datetime format
  178. *
  179. * @return a date in MySQL datetime format
  180. * @link http://dev.mysql.com/doc/refman/4.1/en/datetime.html MySQL DATETIME
  181. * format
  182. */
  183. function toMySQL($local = false)
  184. {
  185. $date = ($local) ? $this->_date + $this->_offset : $this->_date;
  186. $date = ($this->_date !== false) ? date('Y-m-d H:i:s', $date) : null;
  187. return $date;
  188. }
  189. /**
  190. * Gets the date as UNIX time stamp.
  191. *
  192. * @return a date as a unix time stamp
  193. */
  194. function toUnix($local = false)
  195. {
  196. $date = null;
  197. if ($this->_date !== false) {
  198. $date = ($local) ? $this->_date + $this->_offset : $this->_date;
  199. }
  200. return $date;
  201. }
  202. /**
  203. * Gets the date in a specific format
  204. *
  205. * Returns a string formatted according to the given format. Month and weekday names and
  206. * other language dependent strings respect the current locale
  207. *
  208. * @param string $format The date format specification string (see {@link PHP_MANUAL#strftime})
  209. * @return a date in a specific format
  210. */
  211. function toFormat($format = '%Y-%m-%d %H:%M:%S')
  212. {
  213. $date = ($this->_date !== false) ? $this->_strftime($format, $this->_date + $this->_offset) : null;
  214. return $date;
  215. }
  216. /**
  217. * Translates needed strings in for JDate::toFormat (see {@link PHP_MANUAL#strftime})
  218. *
  219. * @access protected
  220. * @param string $format The date format specification string (see {@link PHP_MANUAL#strftime})
  221. * @param int $time Unix timestamp
  222. * @return string a date in the specified format
  223. */
  224. function _strftime($format, $time)
  225. {
  226. if(strpos($format, '%a') !== false)
  227. $format = str_replace('%a', $this->_dayToString(date('w', $time), true), $format);
  228. if(strpos($format, '%A') !== false)
  229. $format = str_replace('%A', $this->_dayToString(date('w', $time)), $format);
  230. if(strpos($format, '%b') !== false)
  231. $format = str_replace('%b', $this->_monthToString(date('n', $time), true), $format);
  232. if(strpos($format, '%B') !== false)
  233. $format = str_replace('%B', $this->_monthToString(date('n', $time)), $format);
  234. $date = strftime($format, $time);
  235. return $date;
  236. }
  237. /**
  238. * Translates month number to string
  239. *
  240. * @access protected
  241. * @param int $month The numeric month of the year
  242. * @param bool $abbr Return the abreviated month string?
  243. * @return string month string
  244. */
  245. function _monthToString($month, $abbr = false)
  246. {
  247. switch ($month)
  248. {
  249. case 1: return $abbr ? JText::_('JANUARY_SHORT') : JText::_('JANUARY');
  250. case 2: return $abbr ? JText::_('FEBRUARY_SHORT') : JText::_('FEBRUARY');
  251. case 3: return $abbr ? JText::_('MARCH_SHORT') : JText::_('MARCH');
  252. case 4: return $abbr ? JText::_('APRIL_SHORT') : JText::_('APRIL');
  253. case 5: return $abbr ? JText::_('MAY_SHORT') : JText::_('MAY');
  254. case 6: return $abbr ? JText::_('JUNE_SHORT') : JText::_('JUNE');
  255. case 7: return $abbr ? JText::_('JULY_SHORT') : JText::_('JULY');
  256. case 8: return $abbr ? JText::_('AUGUST_SHORT') : JText::_('AUGUST');
  257. case 9: return $abbr ? JText::_('SEPTEMBER_SHORT') : JText::_('SEPTEMBER');
  258. case 10: return $abbr ? JText::_('OCTOBER_SHORT') : JText::_('OCTOBER');
  259. case 11: return $abbr ? JText::_('NOVEMBER_SHORT') : JText::_('NOVEMBER');
  260. case 12: return $abbr ? JText::_('DECEMBER_SHORT') : JText::_('DECEMBER');
  261. }
  262. }
  263. /**
  264. * Translates day of week number to string
  265. *
  266. * @access protected
  267. * @param int $day The numeric day of the week
  268. * @param bool $abbr Return the abreviated day string?
  269. * @return string day string
  270. */
  271. function _dayToString($day, $abbr = false)
  272. {
  273. switch ($day)
  274. {
  275. case 0: return $abbr ? JText::_('SUN') : JText::_('SUNDAY');
  276. case 1: return $abbr ? JText::_('MON') : JText::_('MONDAY');
  277. case 2: return $abbr ? JText::_('TUE') : JText::_('TUESDAY');
  278. case 3: return $abbr ? JText::_('WED') : JText::_('WEDNESDAY');
  279. case 4: return $abbr ? JText::_('THU') : JText::_('THURSDAY');
  280. case 5: return $abbr ? JText::_('FRI') : JText::_('FRIDAY');
  281. case 6: return $abbr ? JText::_('SAT') : JText::_('SATURDAY');
  282. }
  283. }
  284. }