PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/bonfire/helpers/BF_date_helper.php

http://github.com/ci-bonfire/Bonfire
PHP | 266 lines | 166 code | 21 blank | 79 comment | 26 complexity | 636298e3e7c4a0c3fe65bd4fda001fa7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('BASEPATH') || exit('No direct script access allowed');
  2. /**
  3. * Bonfire
  4. *
  5. * An open source project to allow developers to jumpstart their development of
  6. * CodeIgniter applications.
  7. *
  8. * @package Bonfire
  9. * @author Bonfire Dev Team
  10. * @copyright Copyright (c) 2011 - 2018, Bonfire Dev Team
  11. * @license http://opensource.org/licenses/MIT The MIT License
  12. * @link http://cibonfire.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. /**
  17. * Date helper functions.
  18. *
  19. * Includes additional date-related functions helpful in Bonfire development.
  20. *
  21. * @package Bonfire\Helpers\BF_date_helper
  22. * @author Bonfire Dev Team
  23. * @link http://cibonfire.com/docs/developer
  24. */
  25. if (! function_exists('date_difference')) {
  26. /**
  27. * Return the difference between two dates.
  28. *
  29. * @todo Consider updating this to use date_diff() and/or DateInterval.
  30. *
  31. * @param mixed $start The start date as a unix timestamp or in a format
  32. * which can be used within strtotime().
  33. * @param mixed $end The ending date as a unix timestamp or in a
  34. * format which can be used within strtotime().
  35. * @param string $interval A string with the interval to use. Valid values
  36. * are 'week', 'day', 'hour', or 'minute'.
  37. * @param bool $reformat If TRUE, will reformat the time using strtotime().
  38. *
  39. * @return int A number representing the difference between the two dates in
  40. * the interval desired.
  41. */
  42. function date_difference($start = null, $end = null, $interval = 'day', $reformat = false)
  43. {
  44. if (is_null($start)) {
  45. return false;
  46. }
  47. if (is_null($end)) {
  48. $end = date('Y-m-d H:i:s');
  49. }
  50. $times = array(
  51. 'week' => 604800,
  52. 'day' => 86400,
  53. 'hour' => 3600,
  54. 'minute' => 60,
  55. );
  56. if ($reformat === true) {
  57. $start = strtotime($start);
  58. $end = strtotime($end);
  59. }
  60. $diff = $end - $start;
  61. return round($diff / $times[$interval]);
  62. }
  63. }
  64. if (! function_exists('relative_time')) {
  65. /**
  66. * Return a string representing how long ago a given UNIX timestamp was,
  67. * e.g. "moments ago", "2 weeks ago", etc.
  68. *
  69. * @todo Consider updating this to use date_diff() and/or DateInterval.
  70. * @todo Internationalization.
  71. *
  72. * @param int $timestamp A UNIX timestamp.
  73. *
  74. * @return string A human-readable amount of time 'ago'.
  75. */
  76. function relative_time($timestamp)
  77. {
  78. if ($timestamp != '' && ! is_int($timestamp)) {
  79. $timestamp = strtotime($timestamp);
  80. }
  81. if (! is_int($timestamp)) {
  82. return "never";
  83. }
  84. $difference = time() - $timestamp;
  85. $periods = array('moment', 'min', 'hour', 'day', 'week', 'month', 'year', 'decade');
  86. $lengths = array('60', '60', '24', '7', '4.35', '12', '10', '10');
  87. if ($difference >= 0) {
  88. // This was in the past
  89. $ending = "ago";
  90. } else {
  91. // This is in the future
  92. $difference = -$difference;
  93. $ending = "to go";
  94. }
  95. for ($j = 0; $difference >= $lengths[$j]; $j++) {
  96. $difference /= $lengths[$j];
  97. }
  98. $difference = round($difference);
  99. if ($difference != 1) {
  100. $periods[$j] .= "s";
  101. }
  102. if ($difference < 60 && $j == 0) {
  103. return "{$periods[$j]} {$ending}";
  104. }
  105. return "{$difference} {$periods[$j]} {$ending}";
  106. }
  107. }
  108. if (! function_exists('standard_timezone')) {
  109. /**
  110. * Convert CodeIgniter's time zone strings to standard PHP time zone strings.
  111. *
  112. * @param string $ciTimezone A time zone string generated by CodeIgniter.
  113. *
  114. * @return string A PHP time zone string.
  115. */
  116. function standard_timezone($ciTimezone)
  117. {
  118. switch ($ciTimezone) {
  119. case 'UM12':
  120. return 'Pacific/Kwajalein';
  121. case 'UM11':
  122. return 'Pacific/Midway';
  123. case 'UM10':
  124. return 'Pacific/Honolulu';
  125. case 'UM95':
  126. return 'Pacific/Marquesas';
  127. case 'UM9':
  128. return 'Pacific/Gambier';
  129. case 'UM8':
  130. return 'America/Los_Angeles';
  131. case 'UM7':
  132. return 'America/Boise';
  133. case 'UM6':
  134. return 'America/Chicago';
  135. case 'UM5':
  136. return 'America/New_York';
  137. case 'UM45':
  138. return 'America/Caracas';
  139. case 'UM4':
  140. return 'America/Sao_Paulo';
  141. case 'UM35':
  142. return 'America/St_Johns';
  143. case 'UM3':
  144. return 'America/Buenos_Aires';
  145. case 'UM2':
  146. return 'Atlantic/St_Helena';
  147. case 'UM1':
  148. return 'Atlantic/Azores';
  149. case 'UP1':
  150. return 'Europe/Berlin';
  151. case 'UP2':
  152. return 'Europe/Kaliningrad';
  153. case 'UP3':
  154. return 'Asia/Baghdad';
  155. case 'UP35':
  156. return 'Asia/Tehran';
  157. case 'UP4':
  158. return 'Asia/Baku';
  159. case 'UP45':
  160. return 'Asia/Kabul';
  161. case 'UP5':
  162. return 'Asia/Karachi';
  163. case 'UP55':
  164. return 'Asia/Calcutta';
  165. case 'UP575':
  166. return 'Asia/Kathmandu';
  167. case 'UP6':
  168. return 'Asia/Almaty';
  169. case 'UP65':
  170. return 'Asia/Rangoon';
  171. case 'UP7':
  172. return 'Asia/Bangkok';
  173. case 'UP8':
  174. return 'Asia/Hong_Kong';
  175. case 'UP875':
  176. return 'Australia/Eucla';
  177. case 'UP9':
  178. return 'Asia/Tokyo';
  179. case 'UP95':
  180. return 'Australia/Darwin';
  181. case 'UP10':
  182. return 'Australia/Melbourne';
  183. case 'UP105':
  184. return 'Australia/LHI';
  185. case 'UP11':
  186. return 'Asia/Magadan';
  187. case 'UP115':
  188. return 'Pacific/Norfolk';
  189. case 'UP12':
  190. return 'Pacific/Fiji';
  191. case 'UP1275':
  192. return 'Pacific/Chatham';
  193. case 'UP13':
  194. return 'Pacific/Samoa';
  195. case 'UP14':
  196. return 'Pacific/Kiritimati';
  197. case 'UTC':
  198. // no break;
  199. default:
  200. return 'UTC';
  201. }
  202. }
  203. }
  204. if (! function_exists('user_time')) {
  205. /**
  206. * Convert unix time to a human readable time in the user's timezone or in a
  207. * given timezone.
  208. *
  209. * For supported timezones visit - http://php.net/manual/timezones.php
  210. * For accepted formats visit - http://php.net/manual/function.date.php
  211. *
  212. * @example echo user_time();
  213. * @example echo user_time($timestamp, 'EET', 'l jS \of F Y h:i:s A');
  214. *
  215. * @param int $timestamp A UNIX timestamp. If none is given, current time
  216. * will be used.
  217. * @param string $timezone The destination timezone for the conversion. If
  218. * none is given, the current user's configured timezone will be used.
  219. * @param string $format The format string to apply to the converted
  220. * timestamp.
  221. *
  222. * @return string A string containing the timestamp in the requested format.
  223. */
  224. function user_time($timestamp = null, $timezone = null, $format = 'r')
  225. {
  226. if (! $timezone) {
  227. $CI =& get_instance();
  228. $CI->load->library('users/auth');
  229. if ($CI->auth->is_logged_in()) {
  230. $timezone = standard_timezone($CI->auth->user()->timezone);
  231. }
  232. }
  233. if (empty($timestamp)) {
  234. $dtime = new DateTime(null, new DateTimeZone($timezone));
  235. return $dtime->format($format);
  236. }
  237. $dtime = new DateTime();
  238. if (is_int($timestamp)) {
  239. $dtime->setTimestamp($timestamp);
  240. } elseif ($timestamp != 'now') {
  241. $dtime->modify($timestamp);
  242. }
  243. return $dtime->setTimezone(new DateTimeZone($timezone))->format($format);
  244. }
  245. }