Date.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. {
  31. // Use the default timezone
  32. $local = date_default_timezone_get();
  33. }
  34. if (is_int($now))
  35. {
  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. {
  68. // Invalid output
  69. return FALSE;
  70. }
  71. // Array with the output formats
  72. $output = preg_split('/[^a-z]+/', $output);
  73. // Convert the list of outputs to an associative array
  74. $output = array_combine($output, array_fill(0, count($output), 0));
  75. // Make the output values into keys
  76. extract(array_flip($output), EXTR_SKIP);
  77. if ($local === NULL)
  78. {
  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. {
  86. $timespan -= self::YEAR * ($output['years'] = (int) floor($timespan / self::YEAR));
  87. }
  88. if (isset($output['months']))
  89. {
  90. $timespan -= self::MONTH * ($output['months'] = (int) floor($timespan / self::MONTH));
  91. }
  92. if (isset($output['weeks']))
  93. {
  94. $timespan -= self::WEEK * ($output['weeks'] = (int) floor($timespan / self::WEEK));
  95. }
  96. if (isset($output['days']))
  97. {
  98. $timespan -= self::DAY * ($output['days'] = (int) floor($timespan / self::DAY));
  99. }
  100. if (isset($output['hours']))
  101. {
  102. $timespan -= self::HOUR * ($output['hours'] = (int) floor($timespan / self::HOUR));
  103. }
  104. if (isset($output['minutes']))
  105. {
  106. $timespan -= self::MINUTE * ($output['minutes'] = (int) floor($timespan / self::MINUTE));
  107. }
  108. // Seconds ago, 1
  109. if (isset($output['seconds']))
  110. {
  111. $output['seconds'] = $timespan;
  112. }
  113. if (count($output) === 1)
  114. {
  115. // Only a single output was requested, return it
  116. return array_pop($output);
  117. }
  118. // Return array
  119. return $output;
  120. }
  121. /**
  122. * 格式化 UNIX 时间戳为人易读的字符串
  123. *
  124. * @param int Unix 时间戳
  125. * @param mixed $local 本地时间
  126. *
  127. * @return string 格式化的日期字符串
  128. */
  129. public static function human($remote, $local = null)
  130. {
  131. $timediff = (is_null($local) || $local ? time() : $local) - $remote;
  132. $chunks = array(
  133. array(60 * 60 * 24 * 365, 'year'),
  134. array(60 * 60 * 24 * 30, 'month'),
  135. array(60 * 60 * 24 * 7, 'week'),
  136. array(60 * 60 * 24, 'day'),
  137. array(60 * 60, 'hour'),
  138. array(60, 'minute'),
  139. array(1, 'second')
  140. );
  141. for ($i = 0, $j = count($chunks); $i < $j; $i++)
  142. {
  143. $seconds = $chunks[$i][0];
  144. $name = $chunks[$i][1];
  145. if (($count = floor($timediff / $seconds)) != 0)
  146. {
  147. break;
  148. }
  149. }
  150. return __("%d {$name}%s ago", $count, ($count > 1 ? 's' : ''));
  151. }
  152. /**
  153. * 获取一个基于时间偏移的Unix时间戳
  154. *
  155. * @param string $type 时间类型,默认为day,可选minute,hour,day,week,month,quarter,year
  156. * @param int $offset 时间偏移量 默认为0,正数表示当前type之后,负数表示当前type之前
  157. * @param string $position 时间的开始或结束,默认为begin,可选前(begin,start,first,front),end
  158. * @param int $year 基准年,默认为null,即以当前年为基准
  159. * @param int $month 基准月,默认为null,即以当前月为基准
  160. * @param int $day 基准天,默认为null,即以当前天为基准
  161. * @param int $hour 基准小时,默认为null,即以当前年小时基准
  162. * @param int $minute 基准分钟,默认为null,即以当前分钟为基准
  163. * @return int 处理后的Unix时间戳
  164. */
  165. public static function unixtime($type = 'day', $offset = 0, $position = 'begin', $year = null, $month = null, $day = null, $hour = null, $minute = null)
  166. {
  167. $year = is_null($year) ? date('Y') : $year;
  168. $month = is_null($month) ? date('m') : $month;
  169. $day = is_null($day) ? date('d') : $day;
  170. $hour = is_null($hour) ? date('H') : $hour;
  171. $minute = is_null($minute) ? date('i') : $minute;
  172. $position = in_array($position, array('begin', 'start', 'first', 'front'));
  173. switch ($type)
  174. {
  175. case 'minute':
  176. $time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year);
  177. break;
  178. case 'hour':
  179. $time = $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year);
  180. break;
  181. case 'day':
  182. $time = $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year);
  183. break;
  184. case 'week':
  185. $time = $position ?
  186. mktime(0, 0, 0, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 1 - 7 * (-$offset), $year) :
  187. mktime(23, 59, 59, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 7 - 7 * (-$offset), $year);
  188. break;
  189. case 'month':
  190. $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);
  191. break;
  192. case 'quarter':
  193. $time = $position ?
  194. mktime(0, 0, 0, 1 + ((ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) - 1) * 3, 1, $year) :
  195. 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);
  196. break;
  197. case 'year':
  198. $time = $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset);
  199. break;
  200. default:
  201. $time = mktime($hour, $minute, 0, $month, $day, $year);
  202. break;
  203. }
  204. return $time;
  205. }
  206. }