PageRenderTime 41ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/date_helper.php

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