PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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