Date.php 8.3 KB

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