PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/gantry/core/utilities/gantrydate.class.php

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