Date.php 8.6 KB

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