PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Gallary/system/helpers/date.php

https://bitbucket.org/JakePratt/kupcakz.com
PHP | 395 lines | 182 code | 65 blank | 148 comment | 20 complexity | 8217d6f43518b2f6325959ddcb242265 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Date helper class.
  4. *
  5. * @package Kohana
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2009 Kohana Team
  8. * @license http://kohanaphp.com/license
  9. */
  10. class date_Core {
  11. /**
  12. * Converts a UNIX timestamp to DOS format.
  13. *
  14. * @param integer UNIX timestamp
  15. * @return integer
  16. */
  17. public static function unix2dos($timestamp = FALSE)
  18. {
  19. $timestamp = ($timestamp === FALSE) ? getdate() : getdate($timestamp);
  20. if ($timestamp['year'] < 1980)
  21. {
  22. return (1 << 21 | 1 << 16);
  23. }
  24. $timestamp['year'] -= 1980;
  25. // What voodoo is this? I have no idea... Geert can explain it though,
  26. // and that's good enough for me.
  27. return ($timestamp['year'] << 25 | $timestamp['mon'] << 21 |
  28. $timestamp['mday'] << 16 | $timestamp['hours'] << 11 |
  29. $timestamp['minutes'] << 5 | $timestamp['seconds'] >> 1);
  30. }
  31. /**
  32. * Converts a DOS timestamp to UNIX format.
  33. *
  34. * @param integer DOS timestamp
  35. * @return integer
  36. */
  37. public static function dos2unix($timestamp = FALSE)
  38. {
  39. $sec = 2 * ($timestamp & 0x1f);
  40. $min = ($timestamp >> 5) & 0x3f;
  41. $hrs = ($timestamp >> 11) & 0x1f;
  42. $day = ($timestamp >> 16) & 0x1f;
  43. $mon = ($timestamp >> 21) & 0x0f;
  44. $year = ($timestamp >> 25) & 0x7f;
  45. return mktime($hrs, $min, $sec, $mon, $day, $year + 1980);
  46. }
  47. /**
  48. * Returns the offset (in seconds) between two time zones.
  49. * @see http://php.net/timezones
  50. *
  51. * @param string timezone to find the offset of
  52. * @param string|boolean timezone used as the baseline
  53. * @param string time at which to calculate
  54. * @return integer
  55. */
  56. public static function offset($remote, $local = TRUE, $when = 'now')
  57. {
  58. if ($local === TRUE)
  59. {
  60. $local = date_default_timezone_get();
  61. }
  62. // Create timezone objects
  63. $remote = new DateTimeZone($remote);
  64. $local = new DateTimeZone($local);
  65. // Create date objects from timezones
  66. $time_there = new DateTime($when, $remote);
  67. $time_here = new DateTime($when, $local);
  68. // Find the offset
  69. return $remote->getOffset($time_there) - $local->getOffset($time_here);
  70. }
  71. /**
  72. * Number of seconds in a minute, incrementing by a step.
  73. *
  74. * @param integer amount to increment each step by, 1 to 30
  75. * @param integer start value
  76. * @param integer end value
  77. * @return array A mirrored (foo => foo) array from 1-60.
  78. */
  79. public static function seconds($step = 1, $start = 0, $end = 60)
  80. {
  81. // Always integer
  82. $step = (int) $step;
  83. $seconds = array();
  84. for ($i = $start; $i < $end; $i += $step)
  85. {
  86. $seconds[$i] = ($i < 10) ? '0'.$i : $i;
  87. }
  88. return $seconds;
  89. }
  90. /**
  91. * Number of minutes in an hour, incrementing by a step.
  92. *
  93. * @param integer amount to increment each step by, 1 to 30
  94. * @return array A mirrored (foo => foo) array from 1-60.
  95. */
  96. public static function minutes($step = 5)
  97. {
  98. // Because there are the same number of minutes as seconds in this set,
  99. // we choose to re-use seconds(), rather than creating an entirely new
  100. // function. Shhhh, it's cheating! ;) There are several more of these
  101. // in the following methods.
  102. return date::seconds($step);
  103. }
  104. /**
  105. * Number of hours in a day.
  106. *
  107. * @param integer amount to increment each step by
  108. * @param boolean use 24-hour time
  109. * @param integer the hour to start at
  110. * @return array A mirrored (foo => foo) array from start-12 or start-23.
  111. */
  112. public static function hours($step = 1, $long = FALSE, $start = NULL)
  113. {
  114. // Default values
  115. $step = (int) $step;
  116. $long = (bool) $long;
  117. $hours = array();
  118. // Set the default start if none was specified.
  119. if ($start === NULL)
  120. {
  121. $start = ($long === FALSE) ? 1 : 0;
  122. }
  123. $hours = array();
  124. // 24-hour time has 24 hours, instead of 12
  125. $size = ($long === TRUE) ? 23 : 12;
  126. for ($i = $start; $i <= $size; $i += $step)
  127. {
  128. $hours[$i] = $i;
  129. }
  130. return $hours;
  131. }
  132. /**
  133. * Returns AM or PM, based on a given hour.
  134. *
  135. * @param integer number of the hour
  136. * @return string
  137. */
  138. public static function ampm($hour)
  139. {
  140. // Always integer
  141. $hour = (int) $hour;
  142. return ($hour > 11) ? 'PM' : 'AM';
  143. }
  144. /**
  145. * Adjusts a non-24-hour number into a 24-hour number.
  146. *
  147. * @param integer hour to adjust
  148. * @param string AM or PM
  149. * @return string
  150. */
  151. public static function adjust($hour, $ampm)
  152. {
  153. $hour = (int) $hour;
  154. $ampm = strtolower($ampm);
  155. switch ($ampm)
  156. {
  157. case 'am':
  158. if ($hour == 12)
  159. $hour = 0;
  160. break;
  161. case 'pm':
  162. if ($hour < 12)
  163. $hour += 12;
  164. break;
  165. }
  166. return sprintf('%02s', $hour);
  167. }
  168. /**
  169. * Number of days in month.
  170. *
  171. * @param integer number of month
  172. * @param integer number of year to check month, defaults to the current year
  173. * @return array A mirrored (foo => foo) array of the days.
  174. */
  175. public static function days($month, $year = FALSE)
  176. {
  177. static $months;
  178. // Always integers
  179. $month = (int) $month;
  180. $year = (int) $year;
  181. // Use the current year by default
  182. $year = ($year == FALSE) ? date('Y') : $year;
  183. // We use caching for months, because time functions are used
  184. if (empty($months[$year][$month]))
  185. {
  186. $months[$year][$month] = array();
  187. // Use date to find the number of days in the given month
  188. $total = date('t', mktime(1, 0, 0, $month, 1, $year)) + 1;
  189. for ($i = 1; $i < $total; $i++)
  190. {
  191. $months[$year][$month][$i] = $i;
  192. }
  193. }
  194. return $months[$year][$month];
  195. }
  196. /**
  197. * Number of months in a year
  198. *
  199. * @return array A mirrored (foo => foo) array from 1-12.
  200. */
  201. public static function months()
  202. {
  203. return date::hours();
  204. }
  205. /**
  206. * Returns an array of years between a starting and ending year.
  207. * Uses the current year +/- 5 as the max/min.
  208. *
  209. * @param integer starting year
  210. * @param integer ending year
  211. * @return array
  212. */
  213. public static function years($start = FALSE, $end = FALSE)
  214. {
  215. // Default values
  216. $start = ($start === FALSE) ? date('Y') - 5 : (int) $start;
  217. $end = ($end === FALSE) ? date('Y') + 5 : (int) $end;
  218. $years = array();
  219. // Add one, so that "less than" works
  220. $end += 1;
  221. for ($i = $start; $i < $end; $i++)
  222. {
  223. $years[$i] = $i;
  224. }
  225. return $years;
  226. }
  227. /**
  228. * Returns time difference between two timestamps, in human readable format.
  229. *
  230. * @param integer timestamp
  231. * @param integer timestamp, defaults to the current time
  232. * @param string formatting string
  233. * @return string|array
  234. */
  235. public static function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
  236. {
  237. // Array with the output formats
  238. $output = preg_split('/[^a-z]+/', strtolower((string) $output));
  239. // Invalid output
  240. if (empty($output))
  241. return FALSE;
  242. // Make the output values into keys
  243. extract(array_flip($output), EXTR_SKIP);
  244. // Default values
  245. $time1 = max(0, (int) $time1);
  246. $time2 = empty($time2) ? time() : max(0, (int) $time2);
  247. // Calculate timespan (seconds)
  248. $timespan = abs($time1 - $time2);
  249. // All values found using Google Calculator.
  250. // Years and months do not match the formula exactly, due to leap years.
  251. // Years ago, 60 * 60 * 24 * 365
  252. isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926));
  253. // Months ago, 60 * 60 * 24 * 30
  254. isset($months) and $timespan -= 2629744 * ($months = (int) floor($timespan / 2629743.83));
  255. // Weeks ago, 60 * 60 * 24 * 7
  256. isset($weeks) and $timespan -= 604800 * ($weeks = (int) floor($timespan / 604800));
  257. // Days ago, 60 * 60 * 24
  258. isset($days) and $timespan -= 86400 * ($days = (int) floor($timespan / 86400));
  259. // Hours ago, 60 * 60
  260. isset($hours) and $timespan -= 3600 * ($hours = (int) floor($timespan / 3600));
  261. // Minutes ago, 60
  262. isset($minutes) and $timespan -= 60 * ($minutes = (int) floor($timespan / 60));
  263. // Seconds ago, 1
  264. isset($seconds) and $seconds = $timespan;
  265. // Remove the variables that cannot be accessed
  266. unset($timespan, $time1, $time2);
  267. // Deny access to these variables
  268. $deny = array_flip(array('deny', 'key', 'difference', 'output'));
  269. // Return the difference
  270. $difference = array();
  271. foreach ($output as $key)
  272. {
  273. if (isset($$key) AND ! isset($deny[$key]))
  274. {
  275. // Add requested key to the output
  276. $difference[$key] = $$key;
  277. }
  278. }
  279. // Invalid output formats string
  280. if (empty($difference))
  281. return FALSE;
  282. // If only one output format was asked, don't put it in an array
  283. if (count($difference) === 1)
  284. return current($difference);
  285. // Return array
  286. return $difference;
  287. }
  288. /**
  289. * Returns time difference between two timestamps, in the format:
  290. * N year, N months, N weeks, N days, N hours, N minutes, and N seconds ago
  291. *
  292. * @param integer timestamp
  293. * @param integer timestamp, defaults to the current time
  294. * @param string formatting string
  295. * @return string
  296. */
  297. public static function timespan_string($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
  298. {
  299. if ($difference = date::timespan($time1, $time2, $output) AND is_array($difference))
  300. {
  301. // Determine the key of the last item in the array
  302. $last = end($difference);
  303. $last = key($difference);
  304. $span = array();
  305. foreach ($difference as $name => $amount)
  306. {
  307. if ($amount === 0)
  308. {
  309. // Skip empty amounts
  310. continue;
  311. }
  312. // Add the amount to the span
  313. $span[] = ($name === $last ? ' and ' : ', ').$amount.' '.($amount === 1 ? inflector::singular($name) : $name);
  314. }
  315. // If the difference is less than 60 seconds, remove the preceding and.
  316. if (count($span) === 1)
  317. {
  318. $span[0] = ltrim($span[0], 'and ');
  319. }
  320. // Replace difference by making the span into a string
  321. $difference = trim(implode('', $span), ',');
  322. }
  323. elseif (is_int($difference))
  324. {
  325. // Single-value return
  326. $difference = $difference.' '.($difference === 1 ? inflector::singular($output) : $output);
  327. }
  328. return $difference;
  329. }
  330. } // End date