Time.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * 时间处理类
  4. *
  5. * @author Coeus <r.anerg@gmail.com>
  6. */
  7. namespace huolib\tool;
  8. class Time {
  9. /**
  10. * 计算与现在时间差
  11. *
  12. * @param $time
  13. *
  14. * @return false|string
  15. */
  16. public static function optimization($time) {
  17. if (!is_numeric($time)) {
  18. $time = strtotime($time);
  19. }
  20. $_now_time = time();
  21. $d1 = date('d', $time);
  22. $d2 = date('d', $_now_time);
  23. $y1 = date('Y', $time);
  24. $y2 = date('Y', $_now_time);
  25. $htime = date('H:i', $time);
  26. $dif = abs($_now_time - $time);
  27. if ($dif < 10) {
  28. $return = '刚刚';
  29. } else if ($dif < 3600) {
  30. $return = floor($dif / 60).'分钟前';
  31. } else if ($dif < 10800) {
  32. $return = floor($dif / 3600).'小时前';
  33. } else if ($d1 == $d2) {
  34. $return = '今天 '.$htime;
  35. } else if ($dif < 86400) {
  36. $return = '昨天 '.$htime;
  37. } else if ($dif < 172800) {
  38. $return = '前天 '.$htime;
  39. } else if ($y1 == $y2) {
  40. $return = date('m-d H:i', $time);
  41. } else {
  42. $return = date('Y-m-d H:i', $time);
  43. }
  44. return $return;
  45. }
  46. /**
  47. * 返回今日开始和结束的时间戳
  48. *
  49. * @return array
  50. */
  51. public static function today() {
  52. return [
  53. mktime(0, 0, 0, date('m'), date('d'), date('Y')),
  54. mktime(23, 59, 59, date('m'), date('d'), date('Y'))
  55. ];
  56. }
  57. /**
  58. * 返回昨日开始和结束的时间戳
  59. *
  60. * @return array
  61. */
  62. public static function yesterday() {
  63. $yesterday = date('d') - 1;
  64. return [
  65. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  66. mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
  67. ];
  68. }
  69. /**
  70. * 返回本周开始和结束的时间戳
  71. *
  72. * @return array
  73. */
  74. public static function week() {
  75. $timestamp = time();
  76. return [
  77. strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))),
  78. strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1
  79. ];
  80. }
  81. /**
  82. * 返回最近七天开始和结束的时间戳
  83. *
  84. * @return array
  85. */
  86. public static function lastSeven() {
  87. return [
  88. strtotime(date('Y-m-d', strtotime('-6 days'))),
  89. mktime(23, 59, 59, date('m'), date('d'), date('Y'))
  90. ];
  91. }
  92. /**
  93. * 返回上周开始和结束的时间戳
  94. *
  95. * @return array
  96. */
  97. public static function lastWeek() {
  98. $timestamp = time();
  99. return [
  100. strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
  101. strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
  102. ];
  103. }
  104. /**
  105. * 返回本月开始和结束的时间戳
  106. *
  107. * @return array
  108. */
  109. public static function month() {
  110. return [
  111. mktime(0, 0, 0, date('m'), 1, date('Y')),
  112. mktime(23, 59, 59, date('m'), date('t'), date('Y'))
  113. ];
  114. }
  115. /**
  116. * 返回上个月开始和结束的时间戳
  117. *
  118. * @return array
  119. */
  120. public static function lastMonth() {
  121. $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  122. $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));
  123. return [$begin, $end];
  124. }
  125. /**
  126. * 最近30天开始和结束的时间戳
  127. *
  128. * @return array
  129. */
  130. public static function lastThirty() {
  131. $begin = strtotime(date('Y-m-d', strtotime('-30 days')));
  132. $end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
  133. return [$begin, $end];
  134. }
  135. /**
  136. * 返回今年开始和结束的时间戳
  137. *
  138. * @return array
  139. */
  140. public static function year() {
  141. return [
  142. mktime(0, 0, 0, 1, 1, date('Y')),
  143. mktime(23, 59, 59, 12, 31, date('Y'))
  144. ];
  145. }
  146. /**
  147. * 返回去年开始和结束的时间戳
  148. *
  149. * @return array
  150. */
  151. public static function lastYear() {
  152. $year = date('Y') - 1;
  153. return [
  154. mktime(0, 0, 0, 1, 1, $year),
  155. mktime(23, 59, 59, 12, 31, $year)
  156. ];
  157. }
  158. /***
  159. * 获得任意一天的开始时间,结束时间
  160. *
  161. * @param $date
  162. *
  163. * @return array
  164. */
  165. public static function day($date) {
  166. return array(strtotime($date), strtotime($date) + 24 * 3600 - 1);
  167. }
  168. public static function dayOf() {
  169. }
  170. /**
  171. * 获取几天前零点到现在/昨日结束的时间戳
  172. *
  173. * @param int $day 天数
  174. * @param bool $now 返回现在或者昨天结束时间戳
  175. *
  176. * @return array
  177. */
  178. public static function dayToNow($day = 1, $now = true) {
  179. $end = time();
  180. if (!$now) {
  181. list($foo, $end) = self::yesterday();
  182. }
  183. return [
  184. mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
  185. $end
  186. ];
  187. }
  188. /**
  189. * 返回几天前的时间戳
  190. *
  191. * @param int $day
  192. *
  193. * @return int
  194. */
  195. public static function daysAgo($day = 1) {
  196. $nowTime = time();
  197. return $nowTime - self::daysToSecond($day);
  198. }
  199. /**
  200. * 返回几天后的时间戳
  201. *
  202. * @param int $day
  203. *
  204. * @return int
  205. */
  206. public static function daysAfter($day = 1) {
  207. $nowTime = time();
  208. return $nowTime + self::daysToSecond($day);
  209. }
  210. /**
  211. * 天数转换成秒数
  212. *
  213. * @param int $day
  214. *
  215. * @return int
  216. */
  217. public static function daysToSecond($day = 1) {
  218. return $day * 86400;
  219. }
  220. /**
  221. * 周数转换成秒数
  222. *
  223. * @param int $week
  224. *
  225. * @return int
  226. */
  227. public static function weekToSecond($week = 1) {
  228. return self::daysToSecond() * 7 * $week;
  229. }
  230. /**
  231. * 时间相差天数
  232. *
  233. * @param $start_time
  234. * @param $end_time
  235. *
  236. * @return float
  237. */
  238. public static function timeDateDiff($start_time, $end_time) {
  239. $_start_time = strtotime(date('Y-m-d', $start_time));
  240. $_end_time = strtotime(date('Y-m-d', $end_time));
  241. return round(abs($_end_time - $_start_time) / 86400);
  242. }
  243. }