PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/Date.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 299 lines | 173 code | 22 blank | 104 comment | 37 complexity | 33b4164eca72c64d2112914c5e664b0e MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: Date.php 57540 2012-10-14 18:27:59Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} 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('ROKCOMMON') or die('Restricted access');
  14. class RokCommon_Date
  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. date_default_timezone_set('UTC');
  42. if ($date == 'now' || empty($date))
  43. {
  44. $this->_date = strtotime(gmdate("M d Y H:i:s", time()));
  45. return;
  46. }
  47. $tzOffset *= 3600;
  48. if (is_numeric($date))
  49. {
  50. $this->_date = $date - $tzOffset;
  51. return;
  52. }
  53. 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))
  54. {
  55. $months = Array(
  56. 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
  57. 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
  58. 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12
  59. );
  60. $matches[2] = strtolower($matches[2]);
  61. if (! isset($months[$matches[2]])) {
  62. return;
  63. }
  64. $this->_date = mktime(
  65. $matches[4], $matches[5], $matches[6],
  66. $months[$matches[2]], $matches[1], $matches[3]
  67. );
  68. if ($this->_date === false) {
  69. return;
  70. }
  71. if ($matches[7][0] == '+') {
  72. $tzOffset = 3600 * substr($matches[7], 1, 2)
  73. + 60 * substr($matches[7], -2);
  74. } elseif ($matches[7][0] == '-') {
  75. $tzOffset = -3600 * substr($matches[7], 1, 2)
  76. - 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': $tzOffset = 0;
  92. }
  93. }
  94. $this->_date -= $tzOffset;
  95. return;
  96. }
  97. if (preg_match('~^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][ 0-9])(\.[0-9]+)?(Z|([+-](?:2[ 0-3]|[0-1][0-9])):([0-5][0-9]))?$~',$date,$matches))
  98. {
  99. $this->_date = mktime(
  100. $matches[4], $matches[5], $matches[6],
  101. $matches[2], $matches[3], $matches[1]
  102. );
  103. if ($this->_date == false) {
  104. return;
  105. }
  106. if (isset($matches[9][0])) {
  107. if ($matches[9][0] == '+' || $matches[9][0] == '-') {
  108. $partialtz = (isset($matches[10]))?(int)$matches[10]/60:0;
  109. $tzOffset = 60 * 60 * ((int)$matches[9] + $partialtz);
  110. } elseif ($matches[8] == '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 string 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. * @param bool $local
  184. *
  185. * @return string a date as a unix time stamp
  186. */
  187. function toUnix($local = false)
  188. {
  189. $date = null;
  190. if ($this->_date !== false) {
  191. $date = ($local) ? $this->_date + $this->_offset : $this->_date;
  192. }
  193. return $date;
  194. }
  195. /**
  196. * Gets the date in a specific format
  197. *
  198. * Returns a string formatted according to the given format. Month and weekday names and
  199. * other language dependent strings respect the current locale
  200. *
  201. * @param string $format The date format specification string (see {@link PHP_MANUAL#strftime})
  202. * @return a date in a specific format
  203. */
  204. function toFormat($format = '%Y-%m-%d %H:%M:%S')
  205. {
  206. $date = ($this->_date !== false) ? $this->_strftime($format, $this->_date + $this->_offset) : null;
  207. return $date;
  208. }
  209. /**
  210. * Translates needed strings in for JDate::toFormat (see {@link PHP_MANUAL#strftime})
  211. *
  212. * @access protected
  213. * @param string $format The date format specification string (see {@link PHP_MANUAL#strftime})
  214. * @param int $time Unix timestamp
  215. * @return string a date in the specified format
  216. */
  217. function _strftime($format, $time)
  218. {
  219. if(strpos($format, '%a') !== false)
  220. $format = str_replace('%a', $this->_dayToString(date('w', $time), true), $format);
  221. if(strpos($format, '%A') !== false)
  222. $format = str_replace('%A', $this->_dayToString(date('w', $time)), $format);
  223. if(strpos($format, '%b') !== false)
  224. $format = str_replace('%b', $this->_monthToString(date('n', $time), true), $format);
  225. if(strpos($format, '%B') !== false)
  226. $format = str_replace('%B', $this->_monthToString(date('n', $time)), $format);
  227. $date = strftime($format, $time);
  228. return $date;
  229. }
  230. /**
  231. * Translates month number to string
  232. *
  233. * @access protected
  234. * @param int $month The numeric month of the year
  235. * @param bool $abbr Return the abreviated month string?
  236. * @return string month string
  237. */
  238. function _monthToString($month, $abbr = false)
  239. {
  240. switch ($month)
  241. {
  242. case 1: return $abbr ? rc__('JANUARY_SHORT') : rc__('JANUARY');
  243. case 2: return $abbr ? rc__('FEBRUARY_SHORT') : rc__('FEBRUARY');
  244. case 3: return $abbr ? rc__('MARCH_SHORT') : rc__('MARCH');
  245. case 4: return $abbr ? rc__('APRIL_SHORT') : rc__('APRIL');
  246. case 5: return $abbr ? rc__('MAY_SHORT') : rc__('MAY');
  247. case 6: return $abbr ? rc__('JUNE_SHORT') : rc__('JUNE');
  248. case 7: return $abbr ? rc__('JULY_SHORT') : rc__('JULY');
  249. case 8: return $abbr ? rc__('AUGUST_SHORT') : rc__('AUGUST');
  250. case 9: return $abbr ? rc__('SEPTEMBER_SHORT') : rc__('SEPTEMBER');
  251. case 10: return $abbr ? rc__('OCTOBER_SHORT') : rc__('OCTOBER');
  252. case 11: return $abbr ? rc__('NOVEMBER_SHORT') : rc__('NOVEMBER');
  253. case 12: return $abbr ? rc__('DECEMBER_SHORT') : rc__('DECEMBER');
  254. }
  255. }
  256. /**
  257. * Translates day of week number to string
  258. *
  259. * @access protected
  260. * @param int $day The numeric day of the week
  261. * @param bool $abbr Return the abreviated day string?
  262. * @return string day string
  263. */
  264. function _dayToString($day, $abbr = false)
  265. {
  266. switch ($day)
  267. {
  268. case 0: return $abbr ? rc__('SUN') : rc__('SUNDAY');
  269. case 1: return $abbr ? rc__('MON') : rc__('MONDAY');
  270. case 2: return $abbr ? rc__('TUE') : rc__('TUESDAY');
  271. case 3: return $abbr ? rc__('WED') : rc__('WEDNESDAY');
  272. case 4: return $abbr ? rc__('THU') : rc__('THURSDAY');
  273. case 5: return $abbr ? rc__('FRI') : rc__('FRIDAY');
  274. case 6: return $abbr ? rc__('SAT') : JText::_('SATURDAY');
  275. }
  276. }
  277. }