Date.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. $baseTime = mktime(0, 0, 0, $month, $day, $year);
  162. switch ($type) {
  163. case 'minute':
  164. $time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year);
  165. break;
  166. case 'hour':
  167. $time = $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year);
  168. break;
  169. case 'day':
  170. $time = $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year);
  171. break;
  172. case 'week':
  173. $weekIndex = date("w", $baseTime);
  174. $time = $position ?
  175. strtotime($offset . " weeks", strtotime(date('Y-m-d', strtotime("-" . ($weekIndex ? $weekIndex - 1 : 6) . " days", $baseTime)))) :
  176. strtotime($offset . " weeks", strtotime(date('Y-m-d 23:59:59', strtotime("+" . (6 - ($weekIndex ? $weekIndex - 1 : 6)) . " days", $baseTime))));
  177. break;
  178. case 'month':
  179. $_timestamp = mktime(0, 0, 0, $month + $offset, 1, $year);
  180. $time = $position ? $_timestamp : mktime(23, 59, 59, $month + $offset, cal_days_in_month(CAL_GREGORIAN, date("m", $_timestamp), date("Y", $_timestamp)), $year);
  181. break;
  182. case 'quarter':
  183. $_month = date("m", mktime(0, 0, 0, (ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, $day, $year));
  184. $time = $position ?
  185. mktime(0, 0, 0, 1 + ((ceil(date('n', $baseTime) / 3) + $offset) - 1) * 3, 1, $year) :
  186. mktime(23, 59, 59, (ceil(date('n', $baseTime) / 3) + $offset) * 3, cal_days_in_month(CAL_GREGORIAN, (ceil(date('n', $baseTime) / 3) + $offset) * 3, $year), $year);
  187. break;
  188. case 'year':
  189. $time = $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset);
  190. break;
  191. default:
  192. $time = mktime($hour, $minute, 0, $month, $day, $year);
  193. break;
  194. }
  195. return $time;
  196. }
  197. }