Date.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace fast;
  3. use DateTime;
  4. use DateTimeZone;
  5. /**
  6. * 日期时间处理类
  7. */
  8. class Date
  9. {
  10. const YEAR = 31536000;
  11. const MONTH = 2592000;
  12. const WEEK = 604800;
  13. const DAY = 86400;
  14. const HOUR = 3600;
  15. const MINUTE = 60;
  16. /**
  17. * 计算两个时区间相差的时长,单位为秒
  18. *
  19. * $seconds = self::offset('America/Chicago', 'GMT');
  20. *
  21. * [!!] A list of time zones that PHP supports can be found at
  22. * <http://php.net/timezones>.
  23. *
  24. * @param string $remote timezone that to find the offset of
  25. * @param string $local timezone used as the baseline
  26. * @param mixed $now UNIX timestamp or date string
  27. * @return integer
  28. */
  29. public static function offset($remote, $local = null, $now = null)
  30. {
  31. if ($local === null) {
  32. // Use the default timezone
  33. $local = date_default_timezone_get();
  34. }
  35. if (is_int($now)) {
  36. // Convert the timestamp into a string
  37. $now = date(DateTime::RFC2822, $now);
  38. }
  39. // Create timezone objects
  40. $zone_remote = new DateTimeZone($remote);
  41. $zone_local = new DateTimeZone($local);
  42. // Create date objects from timezones
  43. $time_remote = new DateTime($now, $zone_remote);
  44. $time_local = new DateTime($now, $zone_local);
  45. // Find the offset
  46. $offset = $zone_remote->getOffset($time_remote) - $zone_local->getOffset($time_local);
  47. return $offset;
  48. }
  49. /**
  50. * 计算两个时间戳之间相差的时间
  51. *
  52. * $span = self::span(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
  53. * $span = self::span(60, 182, 'minutes'); // 2
  54. *
  55. * @param int $remote timestamp to find the span of
  56. * @param int $local timestamp to use as the baseline
  57. * @param string $output formatting string
  58. * @return string when only a single output is requested
  59. * @return array associative list of all outputs requested
  60. * @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php
  61. */
  62. public static function span($remote, $local = null, $output = 'years,months,weeks,days,hours,minutes,seconds')
  63. {
  64. // Normalize output
  65. $output = trim(strtolower((string)$output));
  66. if (!$output) {
  67. // Invalid output
  68. return false;
  69. }
  70. // Array with the output formats
  71. $output = preg_split('/[^a-z]+/', $output);
  72. // Convert the list of outputs to an associative array
  73. $output = array_combine($output, array_fill(0, count($output), 0));
  74. // Make the output values into keys
  75. extract(array_flip($output), EXTR_SKIP);
  76. if ($local === null) {
  77. // Calculate the span from the current time
  78. $local = time();
  79. }
  80. // Calculate timespan (seconds)
  81. $timespan = abs($remote - $local);
  82. if (isset($output['years'])) {
  83. $timespan -= self::YEAR * ($output['years'] = (int)floor($timespan / self::YEAR));
  84. }
  85. if (isset($output['months'])) {
  86. $timespan -= self::MONTH * ($output['months'] = (int)floor($timespan / self::MONTH));
  87. }
  88. if (isset($output['weeks'])) {
  89. $timespan -= self::WEEK * ($output['weeks'] = (int)floor($timespan / self::WEEK));
  90. }
  91. if (isset($output['days'])) {
  92. $timespan -= self::DAY * ($output['days'] = (int)floor($timespan / self::DAY));
  93. }
  94. if (isset($output['hours'])) {
  95. $timespan -= self::HOUR * ($output['hours'] = (int)floor($timespan / self::HOUR));
  96. }
  97. if (isset($output['minutes'])) {
  98. $timespan -= self::MINUTE * ($output['minutes'] = (int)floor($timespan / self::MINUTE));
  99. }
  100. // Seconds ago, 1
  101. if (isset($output['seconds'])) {
  102. $output['seconds'] = $timespan;
  103. }
  104. if (count($output) === 1) {
  105. // Only a single output was requested, return it
  106. return array_pop($output);
  107. }
  108. // Return array
  109. return $output;
  110. }
  111. /**
  112. * 格式化 UNIX 时间戳为人易读的字符串
  113. *
  114. * @param int Unix 时间戳
  115. * @param mixed $local 本地时间
  116. *
  117. * @return string 格式化的日期字符串
  118. */
  119. public static function human($remote, $local = null)
  120. {
  121. $time_diff = (is_null($local) || $local ? time() : $local) - $remote;
  122. $tense = $time_diff < 0 ? 'after' : 'ago';
  123. $time_diff = abs($time_diff);
  124. $chunks = [
  125. [60 * 60 * 24 * 365, 'year'],
  126. [60 * 60 * 24 * 30, 'month'],
  127. [60 * 60 * 24 * 7, 'week'],
  128. [60 * 60 * 24, 'day'],
  129. [60 * 60, 'hour'],
  130. [60, 'minute'],
  131. [1, 'second']
  132. ];
  133. $name = 'second';
  134. $count = 0;
  135. for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  136. $seconds = $chunks[$i][0];
  137. $name = $chunks[$i][1];
  138. if (($count = floor($time_diff / $seconds)) != 0) {
  139. break;
  140. }
  141. }
  142. return __("%d $name%s $tense", $count, ($count > 1 ? 's' : ''));
  143. }
  144. /**
  145. * 获取一个基于时间偏移的Unix时间戳
  146. *
  147. * @param string $type 时间类型,默认为day,可选minute,hour,day,week,month,quarter,year
  148. * @param int $offset 时间偏移量 默认为0,正数表示当前type之后,负数表示当前type之前
  149. * @param string $position 时间的开始或结束,默认为begin,可选前(begin,start,first,front),end
  150. * @param int $year 基准年,默认为null,即以当前年为基准
  151. * @param int $month 基准月,默认为null,即以当前月为基准
  152. * @param int $day 基准天,默认为null,即以当前天为基准
  153. * @param int $hour 基准小时,默认为null,即以当前年小时基准
  154. * @param int $minute 基准分钟,默认为null,即以当前分钟为基准
  155. * @return int 处理后的Unix时间戳
  156. */
  157. public static function unixtime($type = 'day', $offset = 0, $position = 'begin', $year = null, $month = null, $day = null, $hour = null, $minute = null)
  158. {
  159. $year = is_null($year) ? date('Y') : $year;
  160. $month = is_null($month) ? date('m') : $month;
  161. $day = is_null($day) ? date('d') : $day;
  162. $hour = is_null($hour) ? date('H') : $hour;
  163. $minute = is_null($minute) ? date('i') : $minute;
  164. $position = in_array($position, array('begin', 'start', 'first', 'front'));
  165. $baseTime = mktime(0, 0, 0, $month, $day, $year);
  166. switch ($type) {
  167. case 'minute':
  168. $time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year);
  169. break;
  170. case 'hour':
  171. $time = $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year);
  172. break;
  173. case 'day':
  174. $time = $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year);
  175. break;
  176. case 'week':
  177. $weekIndex = date("w", $baseTime);
  178. $time = $position ?
  179. strtotime($offset . " weeks", strtotime(date('Y-m-d', strtotime("-" . ($weekIndex ? $weekIndex - 1 : 6) . " days", $baseTime)))) :
  180. strtotime($offset . " weeks", strtotime(date('Y-m-d 23:59:59', strtotime("+" . (6 - ($weekIndex ? $weekIndex - 1 : 6)) . " days", $baseTime))));
  181. break;
  182. case 'month':
  183. $_timestamp = mktime(0, 0, 0, $month + $offset, 1, $year);
  184. $time = $position ? $_timestamp : mktime(23, 59, 59, $month + $offset, self::days_in_month(date("m", $_timestamp), date("Y", $_timestamp)), $year);
  185. break;
  186. case 'quarter':
  187. $quarter = ceil(date('n', $baseTime) / 3) + $offset;
  188. $month = $quarter * 3;
  189. $offset_year = ceil($month/12) - 1;
  190. $year = $year + $offset_year;
  191. $month = $month - ($offset_year * 12);
  192. $time = $position ?
  193. mktime(0, 0, 0, $month-2, 1, $year) :
  194. mktime(23, 59, 59, $month, self::days_in_month($month, $year), $year);
  195. break;
  196. case 'year':
  197. $time = $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset);
  198. break;
  199. default:
  200. $time = mktime($hour, $minute, 0, $month, $day, $year);
  201. break;
  202. }
  203. return $time;
  204. }
  205. /**
  206. * 获取指定年月拥有的天数
  207. * @param int $month
  208. * @param int $year
  209. * @return false|int|string
  210. */
  211. public static function days_in_month($month, $year)
  212. {
  213. if (function_exists("cal_days_in_month")) {
  214. return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  215. } else {
  216. return date('t', mktime(0, 0, 0, $month, 1, $year));
  217. }
  218. }
  219. }