You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

203 lines
8.3 KiB

  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. $timediff = (is_null($local) || $local ? time() : $local) - $remote;
  122. $chunks = array(
  123. array(60 * 60 * 24 * 365, 'year'),
  124. array(60 * 60 * 24 * 30, 'month'),
  125. array(60 * 60 * 24 * 7, 'week'),
  126. array(60 * 60 * 24, 'day'),
  127. array(60 * 60, 'hour'),
  128. array(60, 'minute'),
  129. array(1, 'second')
  130. );
  131. for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  132. $seconds = $chunks[$i][0];
  133. $name = $chunks[$i][1];
  134. if (($count = floor($timediff / $seconds)) != 0) {
  135. break;
  136. }
  137. }
  138. return __("%d {$name}%s ago", $count, ($count > 1 ? 's' : ''));
  139. }
  140. /**
  141. * 获取一个基于时间偏移的Unix时间戳
  142. *
  143. * @param string $type 时间类型,默认为day,可选minute,hour,day,week,month,quarter,year
  144. * @param int $offset 时间偏移量 默认为0,正数表示当前type之后,负数表示当前type之前
  145. * @param string $position 时间的开始或结束,默认为begin,可选前(begin,start,first,front),end
  146. * @param int $year 基准年,默认为null,即以当前年为基准
  147. * @param int $month 基准月,默认为null,即以当前月为基准
  148. * @param int $day 基准天,默认为null,即以当前天为基准
  149. * @param int $hour 基准小时,默认为null,即以当前年小时基准
  150. * @param int $minute 基准分钟,默认为null,即以当前分钟为基准
  151. * @return int 处理后的Unix时间戳
  152. */
  153. public static function unixtime($type = 'day', $offset = 0, $position = 'begin', $year = null, $month = null, $day = null, $hour = null, $minute = null)
  154. {
  155. $year = is_null($year) ? date('Y') : $year;
  156. $month = is_null($month) ? date('m') : $month;
  157. $day = is_null($day) ? date('d') : $day;
  158. $hour = is_null($hour) ? date('H') : $hour;
  159. $minute = is_null($minute) ? date('i') : $minute;
  160. $position = in_array($position, array('begin', 'start', 'first', 'front'));
  161. switch ($type) {
  162. case 'minute':
  163. $time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year);
  164. break;
  165. case 'hour':
  166. $time = $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year);
  167. break;
  168. case 'day':
  169. $time = $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year);
  170. break;
  171. case 'week':
  172. $time = $position ?
  173. mktime(0, 0, 0, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 1 - 7 * (-$offset), $year) :
  174. mktime(23, 59, 59, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 7 - 7 * (-$offset), $year);
  175. break;
  176. case 'month':
  177. $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);
  178. break;
  179. case 'quarter':
  180. $time = $position ?
  181. mktime(0, 0, 0, 1 + ((ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) - 1) * 3, 1, $year) :
  182. 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);
  183. break;
  184. case 'year':
  185. $time = $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset);
  186. break;
  187. default:
  188. $time = mktime($hour, $minute, 0, $month, $day, $year);
  189. break;
  190. }
  191. return $time;
  192. }
  193. }