PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/codeigniter/system/helpers/date_helper.php

https://bitbucket.org/mbaily/tremain
PHP | 616 lines | 381 code | 88 blank | 147 comment | 82 complexity | d5d26699115e842bab5fe92d3d069395 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Date Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author EllisLab Dev Team
  23. * @link http://codeigniter.com/user_guide/helpers/date_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Get "now" time
  28. *
  29. * Returns time() or its GMT equivalent based on the config file preference
  30. *
  31. * @access public
  32. * @return integer
  33. */
  34. if ( ! function_exists('now'))
  35. {
  36. function now()
  37. {
  38. $CI =& get_instance();
  39. if (strtolower($CI->config->item('time_reference')) == 'gmt')
  40. {
  41. $now = time();
  42. $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
  43. if (strlen($system_time) < 10)
  44. {
  45. $system_time = time();
  46. log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
  47. }
  48. return $system_time;
  49. }
  50. else
  51. {
  52. return time();
  53. }
  54. }
  55. }
  56. // ------------------------------------------------------------------------
  57. /**
  58. * Convert MySQL Style Datecodes
  59. *
  60. * This function is identical to PHPs date() function,
  61. * except that it allows date codes to be formatted using
  62. * the MySQL style, where each code letter is preceded
  63. * with a percent sign: %Y %m %d etc...
  64. *
  65. * The benefit of doing dates this way is that you don't
  66. * have to worry about escaping your text letters that
  67. * match the date codes.
  68. *
  69. * @access public
  70. * @param string
  71. * @param integer
  72. * @return integer
  73. */
  74. if ( ! function_exists('mdate'))
  75. {
  76. function mdate($datestr = '', $time = '')
  77. {
  78. if ($datestr == '')
  79. return '';
  80. if ($time == '')
  81. $time = now();
  82. $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
  83. return date($datestr, $time);
  84. }
  85. }
  86. // ------------------------------------------------------------------------
  87. /**
  88. * Standard Date
  89. *
  90. * Returns a date formatted according to the submitted standard.
  91. *
  92. * @access public
  93. * @param string the chosen format
  94. * @param integer Unix timestamp
  95. * @return string
  96. */
  97. if ( ! function_exists('standard_date'))
  98. {
  99. function standard_date($fmt = 'DATE_RFC822', $time = '')
  100. {
  101. $formats = array(
  102. 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
  103. 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
  104. 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
  105. 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
  106. 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
  107. 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
  108. 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
  109. 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
  110. 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
  111. );
  112. if ( ! isset($formats[$fmt]))
  113. {
  114. return FALSE;
  115. }
  116. return mdate($formats[$fmt], $time);
  117. }
  118. }
  119. // ------------------------------------------------------------------------
  120. /**
  121. * Timespan
  122. *
  123. * Returns a span of seconds in this format:
  124. * 10 days 14 hours 36 minutes 47 seconds
  125. *
  126. * @access public
  127. * @param integer a number of seconds
  128. * @param integer Unix timestamp
  129. * @return integer
  130. */
  131. if ( ! function_exists('timespan'))
  132. {
  133. function timespan($seconds = 1, $time = '')
  134. {
  135. $CI =& get_instance();
  136. $CI->lang->load('date');
  137. // things can get really screwy if a negative number is passed, which can happen
  138. // in very rare load-balanced environments when the web servers' are not in
  139. // perfect sync with one another
  140. $seconds = abs($seconds);
  141. if ( ! is_numeric($seconds))
  142. {
  143. $seconds = 1;
  144. }
  145. if ( ! is_numeric($time))
  146. {
  147. $time = time();
  148. }
  149. if ($time <= $seconds)
  150. {
  151. $seconds = 1;
  152. }
  153. else
  154. {
  155. $seconds = $time - $seconds;
  156. }
  157. $str = '';
  158. $years = floor($seconds / 31536000);
  159. if ($years > 0)
  160. {
  161. $str .= $years.' '.$CI->lang->line((($years > 1) ? 'years' : 'year')).', ';
  162. }
  163. $seconds -= $years * 31536000;
  164. $months = floor($seconds / 2628000);
  165. if ($years > 0 OR $months > 0)
  166. {
  167. if ($months > 0)
  168. {
  169. $str .= $months.' '.$CI->lang->line((($months > 1) ? 'months' : 'month')).', ';
  170. }
  171. $seconds -= $months * 2628000;
  172. }
  173. $weeks = floor($seconds / 604800);
  174. if ($years > 0 OR $months > 0 OR $weeks > 0)
  175. {
  176. if ($weeks > 0)
  177. {
  178. $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'weeks' : 'week')).', ';
  179. }
  180. $seconds -= $weeks * 604800;
  181. }
  182. $days = floor($seconds / 86400);
  183. if ($months > 0 OR $weeks > 0 OR $days > 0)
  184. {
  185. if ($days > 0)
  186. {
  187. $str .= $days.' '.$CI->lang->line((($days > 1) ? 'days' : 'day')).', ';
  188. }
  189. $seconds -= $days * 86400;
  190. }
  191. $hours = floor($seconds / 3600);
  192. if ($days > 0 OR $hours > 0)
  193. {
  194. if ($hours > 0)
  195. {
  196. $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'hours' : 'hour')).', ';
  197. }
  198. $seconds -= $hours * 3600;
  199. }
  200. $minutes = floor($seconds / 60);
  201. if ($days > 0 OR $hours > 0 OR $minutes > 0)
  202. {
  203. if ($minutes > 0)
  204. {
  205. $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'minutes' : 'minute')).', ';
  206. }
  207. $seconds -= $minutes * 60;
  208. }
  209. if ($str == '')
  210. {
  211. $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'seconds' : 'second')).', ';
  212. }
  213. return substr(trim($str), 0, -1);
  214. }
  215. }
  216. // ------------------------------------------------------------------------
  217. /**
  218. * Number of days in a month
  219. *
  220. * Takes a month/year as input and returns the number of days
  221. * for the given month/year. Takes leap years into consideration.
  222. *
  223. * @access public
  224. * @param integer a numeric month
  225. * @param integer a numeric year
  226. * @return integer
  227. */
  228. if ( ! function_exists('days_in_month'))
  229. {
  230. function days_in_month($month = 0, $year = '')
  231. {
  232. if ($month < 1 OR $month > 12)
  233. {
  234. return 0;
  235. }
  236. if ( ! is_numeric($year) OR strlen($year) != 4)
  237. {
  238. $year = date('Y');
  239. }
  240. if ($month == 2)
  241. {
  242. if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
  243. {
  244. return 29;
  245. }
  246. }
  247. $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  248. return $days_in_month[$month - 1];
  249. }
  250. }
  251. // ------------------------------------------------------------------------
  252. /**
  253. * Converts a local Unix timestamp to GMT
  254. *
  255. * @access public
  256. * @param integer Unix timestamp
  257. * @return integer
  258. */
  259. if ( ! function_exists('local_to_gmt'))
  260. {
  261. function local_to_gmt($time = '')
  262. {
  263. if ($time == '')
  264. $time = time();
  265. return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
  266. }
  267. }
  268. // ------------------------------------------------------------------------
  269. /**
  270. * Converts GMT time to a localized value
  271. *
  272. * Takes a Unix timestamp (in GMT) as input, and returns
  273. * at the local value based on the timezone and DST setting
  274. * submitted
  275. *
  276. * @access public
  277. * @param integer Unix timestamp
  278. * @param string timezone
  279. * @param bool whether DST is active
  280. * @return integer
  281. */
  282. if ( ! function_exists('gmt_to_local'))
  283. {
  284. function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
  285. {
  286. if ($time == '')
  287. {
  288. return now();
  289. }
  290. $time += timezones($timezone) * 3600;
  291. if ($dst == TRUE)
  292. {
  293. $time += 3600;
  294. }
  295. return $time;
  296. }
  297. }
  298. // ------------------------------------------------------------------------
  299. /**
  300. * Converts a MySQL Timestamp to Unix
  301. *
  302. * @access public
  303. * @param integer Unix timestamp
  304. * @return integer
  305. */
  306. if ( ! function_exists('mysql_to_unix'))
  307. {
  308. function mysql_to_unix($time = '')
  309. {
  310. // We'll remove certain characters for backward compatibility
  311. // since the formatting changed with MySQL 4.1
  312. // YYYY-MM-DD HH:MM:SS
  313. $time = str_replace('-', '', $time);
  314. $time = str_replace(':', '', $time);
  315. $time = str_replace(' ', '', $time);
  316. // YYYYMMDDHHMMSS
  317. return gmmktime(
  318. substr($time, 8, 2),
  319. substr($time, 10, 2),
  320. substr($time, 12, 2),
  321. substr($time, 4, 2),
  322. substr($time, 6, 2),
  323. substr($time, 0, 4)
  324. );
  325. }
  326. }
  327. // ------------------------------------------------------------------------
  328. /**
  329. * Unix to "Human"
  330. *
  331. * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
  332. *
  333. * @access public
  334. * @param integer Unix timestamp
  335. * @param bool whether to show seconds
  336. * @param string format: us or euro
  337. * @return string
  338. */
  339. if ( ! function_exists('unix_to_human'))
  340. {
  341. function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
  342. {
  343. $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
  344. if ($fmt == 'us')
  345. {
  346. $r .= date('h', $time).':'.date('i', $time);
  347. }
  348. else
  349. {
  350. $r .= date('H', $time).':'.date('i', $time);
  351. }
  352. if ($seconds)
  353. {
  354. $r .= ':'.date('s', $time);
  355. }
  356. if ($fmt == 'us')
  357. {
  358. $r .= ' '.date('A', $time);
  359. }
  360. return $r;
  361. }
  362. }
  363. // ------------------------------------------------------------------------
  364. /**
  365. * Convert "human" date to GMT
  366. *
  367. * Reverses the above process
  368. *
  369. * @access public
  370. * @param string format: us or euro
  371. * @return integer
  372. */
  373. if ( ! function_exists('human_to_unix'))
  374. {
  375. function human_to_unix($datestr = '')
  376. {
  377. if ($datestr == '')
  378. {
  379. return FALSE;
  380. }
  381. $datestr = trim($datestr);
  382. $datestr = preg_replace("/\040+/", ' ', $datestr);
  383. if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
  384. {
  385. return FALSE;
  386. }
  387. $split = explode(' ', $datestr);
  388. $ex = explode("-", $split['0']);
  389. $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
  390. $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
  391. $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
  392. $ex = explode(":", $split['1']);
  393. $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
  394. $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
  395. if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
  396. {
  397. $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
  398. }
  399. else
  400. {
  401. // Unless specified, seconds get set to zero.
  402. $sec = '00';
  403. }
  404. if (isset($split['2']))
  405. {
  406. $ampm = strtolower($split['2']);
  407. if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
  408. $hour = $hour + 12;
  409. if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
  410. $hour = '00';
  411. if (strlen($hour) == 1)
  412. $hour = '0'.$hour;
  413. }
  414. return mktime($hour, $min, $sec, $month, $day, $year);
  415. }
  416. }
  417. // ------------------------------------------------------------------------
  418. /**
  419. * Timezone Menu
  420. *
  421. * Generates a drop-down menu of timezones.
  422. *
  423. * @access public
  424. * @param string timezone
  425. * @param string classname
  426. * @param string menu name
  427. * @return string
  428. */
  429. if ( ! function_exists('timezone_menu'))
  430. {
  431. function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
  432. {
  433. $CI =& get_instance();
  434. $CI->lang->load('date');
  435. if ($default == 'GMT')
  436. $default = 'UTC';
  437. $menu = '<select name="'.$name.'"';
  438. if ($class != '')
  439. {
  440. $menu .= ' class="'.$class.'"';
  441. }
  442. $menu .= ">\n";
  443. foreach (timezones() as $key => $val)
  444. {
  445. $selected = ($default == $key) ? " selected='selected'" : '';
  446. $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
  447. }
  448. $menu .= "</select>";
  449. return $menu;
  450. }
  451. }
  452. // ------------------------------------------------------------------------
  453. /**
  454. * Timezones
  455. *
  456. * Returns an array of timezones. This is a helper function
  457. * for various other ones in this library
  458. *
  459. * @access public
  460. * @param string timezone
  461. * @return string
  462. */
  463. if ( ! function_exists('timezones'))
  464. {
  465. function timezones($tz = '')
  466. {
  467. // Note: Don't change the order of these even though
  468. // some items appear to be in the wrong order
  469. $zones = array(
  470. 'UM12' => -12,
  471. 'UM11' => -11,
  472. 'UM10' => -10,
  473. 'UM95' => -9.5,
  474. 'UM9' => -9,
  475. 'UM8' => -8,
  476. 'UM7' => -7,
  477. 'UM6' => -6,
  478. 'UM5' => -5,
  479. 'UM45' => -4.5,
  480. 'UM4' => -4,
  481. 'UM35' => -3.5,
  482. 'UM3' => -3,
  483. 'UM2' => -2,
  484. 'UM1' => -1,
  485. 'UTC' => 0,
  486. 'UP1' => +1,
  487. 'UP2' => +2,
  488. 'UP3' => +3,
  489. 'UP35' => +3.5,
  490. 'UP4' => +4,
  491. 'UP45' => +4.5,
  492. 'UP5' => +5,
  493. 'UP55' => +5.5,
  494. 'UP575' => +5.75,
  495. 'UP6' => +6,
  496. 'UP65' => +6.5,
  497. 'UP7' => +7,
  498. 'UP8' => +8,
  499. 'UP875' => +8.75,
  500. 'UP9' => +9,
  501. 'UP95' => +9.5,
  502. 'UP10' => +10,
  503. 'UP105' => +10.5,
  504. 'UP11' => +11,
  505. 'UP115' => +11.5,
  506. 'UP12' => +12,
  507. 'UP1275' => +12.75,
  508. 'UP13' => +13,
  509. 'UP14' => +14
  510. );
  511. if ($tz == '')
  512. {
  513. return $zones;
  514. }
  515. if ($tz == 'GMT')
  516. $tz = 'UTC';
  517. return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
  518. }
  519. }
  520. /* End of file date_helper.php */
  521. /* Location: ./system/helpers/date_helper.php */