PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/date_helper.php

https://gitlab.com/fredec/ionizecms-1.0.8.x
PHP | 611 lines | 435 code | 48 blank | 128 comment | 44 complexity | aa1835b0d874161eeff51f2bba11ccc0 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 ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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 ExpressionEngine 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. if ( ! function_exists('now'))
  34. {
  35. function now()
  36. {
  37. $CI =& get_instance();
  38. if (strtolower($CI->config->item('time_reference')) == 'gmt')
  39. {
  40. $now = time();
  41. $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
  42. if (strlen($system_time) < 10)
  43. {
  44. $system_time = time();
  45. log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
  46. }
  47. return $system_time;
  48. }
  49. else
  50. {
  51. return time();
  52. }
  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. if ( ! is_numeric($seconds))
  138. {
  139. $seconds = 1;
  140. }
  141. if ( ! is_numeric($time))
  142. {
  143. $time = time();
  144. }
  145. if ($time <= $seconds)
  146. {
  147. $seconds = 1;
  148. }
  149. else
  150. {
  151. $seconds = $time - $seconds;
  152. }
  153. $str = '';
  154. $years = floor($seconds / 31536000);
  155. if ($years > 0)
  156. {
  157. $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
  158. }
  159. $seconds -= $years * 31536000;
  160. $months = floor($seconds / 2628000);
  161. if ($years > 0 OR $months > 0)
  162. {
  163. if ($months > 0)
  164. {
  165. $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
  166. }
  167. $seconds -= $months * 2628000;
  168. }
  169. $weeks = floor($seconds / 604800);
  170. if ($years > 0 OR $months > 0 OR $weeks > 0)
  171. {
  172. if ($weeks > 0)
  173. {
  174. $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
  175. }
  176. $seconds -= $weeks * 604800;
  177. }
  178. $days = floor($seconds / 86400);
  179. if ($months > 0 OR $weeks > 0 OR $days > 0)
  180. {
  181. if ($days > 0)
  182. {
  183. $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
  184. }
  185. $seconds -= $days * 86400;
  186. }
  187. $hours = floor($seconds / 3600);
  188. if ($days > 0 OR $hours > 0)
  189. {
  190. if ($hours > 0)
  191. {
  192. $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
  193. }
  194. $seconds -= $hours * 3600;
  195. }
  196. $minutes = floor($seconds / 60);
  197. if ($days > 0 OR $hours > 0 OR $minutes > 0)
  198. {
  199. if ($minutes > 0)
  200. {
  201. $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
  202. }
  203. $seconds -= $minutes * 60;
  204. }
  205. if ($str == '')
  206. {
  207. $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
  208. }
  209. return substr(trim($str), 0, -1);
  210. }
  211. }
  212. // ------------------------------------------------------------------------
  213. /**
  214. * Number of days in a month
  215. *
  216. * Takes a month/year as input and returns the number of days
  217. * for the given month/year. Takes leap years into consideration.
  218. *
  219. * @access public
  220. * @param integer a numeric month
  221. * @param integer a numeric year
  222. * @return integer
  223. */
  224. if ( ! function_exists('days_in_month'))
  225. {
  226. function days_in_month($month = 0, $year = '')
  227. {
  228. if ($month < 1 OR $month > 12)
  229. {
  230. return 0;
  231. }
  232. if ( ! is_numeric($year) OR strlen($year) != 4)
  233. {
  234. $year = date('Y');
  235. }
  236. if ($month == 2)
  237. {
  238. if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
  239. {
  240. return 29;
  241. }
  242. }
  243. $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  244. return $days_in_month[$month - 1];
  245. }
  246. }
  247. // ------------------------------------------------------------------------
  248. /**
  249. * Converts a local Unix timestamp to GMT
  250. *
  251. * @access public
  252. * @param integer Unix timestamp
  253. * @return integer
  254. */
  255. if ( ! function_exists('local_to_gmt'))
  256. {
  257. function local_to_gmt($time = '')
  258. {
  259. if ($time == '')
  260. $time = time();
  261. return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
  262. }
  263. }
  264. // ------------------------------------------------------------------------
  265. /**
  266. * Converts GMT time to a localized value
  267. *
  268. * Takes a Unix timestamp (in GMT) as input, and returns
  269. * at the local value based on the timezone and DST setting
  270. * submitted
  271. *
  272. * @access public
  273. * @param integer Unix timestamp
  274. * @param string timezone
  275. * @param bool whether DST is active
  276. * @return integer
  277. */
  278. if ( ! function_exists('gmt_to_local'))
  279. {
  280. function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
  281. {
  282. if ($time == '')
  283. {
  284. return now();
  285. }
  286. $time += timezones($timezone) * 3600;
  287. if ($dst == TRUE)
  288. {
  289. $time += 3600;
  290. }
  291. return $time;
  292. }
  293. }
  294. // ------------------------------------------------------------------------
  295. /**
  296. * Converts a MySQL Timestamp to Unix
  297. *
  298. * @access public
  299. * @param integer Unix timestamp
  300. * @return integer
  301. */
  302. if ( ! function_exists('mysql_to_unix'))
  303. {
  304. function mysql_to_unix($time = '')
  305. {
  306. // We'll remove certain characters for backward compatibility
  307. // since the formatting changed with MySQL 4.1
  308. // YYYY-MM-DD HH:MM:SS
  309. $time = str_replace('-', '', $time);
  310. $time = str_replace(':', '', $time);
  311. $time = str_replace(' ', '', $time);
  312. // YYYYMMDDHHMMSS
  313. return mktime(
  314. substr($time, 8, 2),
  315. substr($time, 10, 2),
  316. substr($time, 12, 2),
  317. substr($time, 4, 2),
  318. substr($time, 6, 2),
  319. substr($time, 0, 4)
  320. );
  321. }
  322. }
  323. // ------------------------------------------------------------------------
  324. /**
  325. * Unix to "Human"
  326. *
  327. * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
  328. *
  329. * @access public
  330. * @param integer Unix timestamp
  331. * @param bool whether to show seconds
  332. * @param string format: us or euro
  333. * @return string
  334. */
  335. if ( ! function_exists('unix_to_human'))
  336. {
  337. function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
  338. {
  339. $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
  340. if ($fmt == 'us')
  341. {
  342. $r .= date('h', $time).':'.date('i', $time);
  343. }
  344. else
  345. {
  346. $r .= date('H', $time).':'.date('i', $time);
  347. }
  348. if ($seconds)
  349. {
  350. $r .= ':'.date('s', $time);
  351. }
  352. if ($fmt == 'us')
  353. {
  354. $r .= ' '.date('A', $time);
  355. }
  356. return $r;
  357. }
  358. }
  359. // ------------------------------------------------------------------------
  360. /**
  361. * Convert "human" date to GMT
  362. *
  363. * Reverses the above process
  364. *
  365. * @access public
  366. * @param string format: us or euro
  367. * @return integer
  368. */
  369. if ( ! function_exists('human_to_unix'))
  370. {
  371. function human_to_unix($datestr = '')
  372. {
  373. if ($datestr == '')
  374. {
  375. return FALSE;
  376. }
  377. $datestr = trim($datestr);
  378. $datestr = preg_replace("/\040+/", ' ', $datestr);
  379. 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))
  380. {
  381. return FALSE;
  382. }
  383. $split = explode(' ', $datestr);
  384. $ex = explode("-", $split['0']);
  385. $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
  386. $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
  387. $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
  388. $ex = explode(":", $split['1']);
  389. $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
  390. $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
  391. if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
  392. {
  393. $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
  394. }
  395. else
  396. {
  397. // Unless specified, seconds get set to zero.
  398. $sec = '00';
  399. }
  400. if (isset($split['2']))
  401. {
  402. $ampm = strtolower($split['2']);
  403. if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
  404. $hour = $hour + 12;
  405. if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
  406. $hour = '00';
  407. if (strlen($hour) == 1)
  408. $hour = '0'.$hour;
  409. }
  410. return mktime($hour, $min, $sec, $month, $day, $year);
  411. }
  412. }
  413. // ------------------------------------------------------------------------
  414. /**
  415. * Timezone Menu
  416. *
  417. * Generates a drop-down menu of timezones.
  418. *
  419. * @access public
  420. * @param string timezone
  421. * @param string classname
  422. * @param string menu name
  423. * @return string
  424. */
  425. if ( ! function_exists('timezone_menu'))
  426. {
  427. function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
  428. {
  429. $CI =& get_instance();
  430. $CI->lang->load('date');
  431. if ($default == 'GMT')
  432. $default = 'UTC';
  433. $menu = '<select name="'.$name.'"';
  434. if ($class != '')
  435. {
  436. $menu .= ' class="'.$class.'"';
  437. }
  438. $menu .= ">\n";
  439. foreach (timezones() as $key => $val)
  440. {
  441. $selected = ($default == $key) ? " selected='selected'" : '';
  442. $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
  443. }
  444. $menu .= "</select>";
  445. return $menu;
  446. }
  447. }
  448. // ------------------------------------------------------------------------
  449. /**
  450. * Timezones
  451. *
  452. * Returns an array of timezones. This is a helper function
  453. * for various other ones in this library
  454. *
  455. * @access public
  456. * @param string timezone
  457. * @return string
  458. */
  459. if ( ! function_exists('timezones'))
  460. {
  461. function timezones($tz = '')
  462. {
  463. // Note: Don't change the order of these even though
  464. // some items appear to be in the wrong order
  465. $zones = array(
  466. 'UM12' => -12,
  467. 'UM11' => -11,
  468. 'UM10' => -10,
  469. 'UM95' => -9.5,
  470. 'UM9' => -9,
  471. 'UM8' => -8,
  472. 'UM7' => -7,
  473. 'UM6' => -6,
  474. 'UM5' => -5,
  475. 'UM45' => -4.5,
  476. 'UM4' => -4,
  477. 'UM35' => -3.5,
  478. 'UM3' => -3,
  479. 'UM2' => -2,
  480. 'UM1' => -1,
  481. 'UTC' => 0,
  482. 'UP1' => +1,
  483. 'UP2' => +2,
  484. 'UP3' => +3,
  485. 'UP35' => +3.5,
  486. 'UP4' => +4,
  487. 'UP45' => +4.5,
  488. 'UP5' => +5,
  489. 'UP55' => +5.5,
  490. 'UP575' => +5.75,
  491. 'UP6' => +6,
  492. 'UP65' => +6.5,
  493. 'UP7' => +7,
  494. 'UP8' => +8,
  495. 'UP875' => +8.75,
  496. 'UP9' => +9,
  497. 'UP95' => +9.5,
  498. 'UP10' => +10,
  499. 'UP105' => +10.5,
  500. 'UP11' => +11,
  501. 'UP115' => +11.5,
  502. 'UP12' => +12,
  503. 'UP1275' => +12.75,
  504. 'UP13' => +13,
  505. 'UP14' => +14
  506. );
  507. if ($tz == '')
  508. {
  509. return $zones;
  510. }
  511. if ($tz == 'GMT')
  512. $tz = 'UTC';
  513. return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
  514. }
  515. }
  516. /* End of file date_helper.php */
  517. /* Location: ./system/helpers/date_helper.php */