Time.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. <?php
  2. /* file:时间处理函数封装的集合
  3. Created by wanghong<1204772286@qq.com>
  4. Date: 2021-02-25 */
  5. namespace utils;
  6. class Time{
  7. /**
  8. * 返回今日开始和结束的时间戳
  9. *
  10. * @return array
  11. */
  12. public static function today()
  13. {
  14. list($y, $m, $d) = explode('-', date('Y-m-d'));
  15. return [
  16. mktime(0, 0, 0, $m, $d, $y),
  17. mktime(23, 59, 59, $m, $d, $y)
  18. ];
  19. }
  20. /**
  21. * 返回昨日开始和结束的时间戳
  22. *
  23. * @return array
  24. */
  25. public static function yesterday()
  26. {
  27. $yesterday = date('d') - 1;
  28. return [
  29. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  30. mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
  31. ];
  32. }
  33. /**
  34. * 返回明日开始和结束的时间戳
  35. *
  36. * @return array
  37. */
  38. public static function tomorrow()
  39. {
  40. $tomorrow = date('d') + 1;
  41. return [
  42. mktime(0, 0, 0, date('m'), $tomorrow, date('Y')),
  43. mktime(23, 59, 59, date('m'), $tomorrow, date('Y'))
  44. ];
  45. }
  46. /**
  47. * 返回本周开始和结束的时间戳
  48. *
  49. * @return array
  50. */
  51. public static function week()
  52. {
  53. list($y, $m, $d, $w) = explode('-', date('Y-m-d-w'));
  54. if($w == 0) $w = 7; //修正周日的问题
  55. return [
  56. mktime(0, 0, 0, $m, $d - $w + 1, $y), mktime(23, 59, 59, $m, $d - $w + 7, $y)
  57. ];
  58. }
  59. /**
  60. * 返回上周开始和结束的时间戳
  61. *
  62. * @return array
  63. */
  64. public static function lastWeek()
  65. {
  66. $timestamp = time();
  67. return [
  68. strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
  69. strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
  70. ];
  71. }
  72. /**
  73. * 返回本月开始和结束的时间戳
  74. *
  75. * @return array
  76. */
  77. public static function month($everyDay = false)
  78. {
  79. list($y, $m, $t) = explode('-', date('Y-m-t'));
  80. return [
  81. mktime(0, 0, 0, $m, 1, $y),
  82. mktime(23, 59, 59, $m, $t, $y)
  83. ];
  84. }
  85. /**
  86. * 返回上个月开始和结束的时间戳
  87. *
  88. * @return array
  89. */
  90. public static function lastMonth()
  91. {
  92. $y = date('Y');
  93. $m = date('m');
  94. $begin = mktime(0, 0, 0, $m - 1, 1, $y);
  95. $end = mktime(23, 59, 59, $m - 1, date('t', $begin), $y);
  96. return [$begin, $end];
  97. }
  98. /**
  99. * 返回今年开始和结束的时间戳
  100. *
  101. * @return array
  102. */
  103. public static function year()
  104. {
  105. $y = date('Y');
  106. return [
  107. mktime(0, 0, 0, 1, 1, $y),
  108. mktime(23, 59, 59, 12, 31, $y)
  109. ];
  110. }
  111. /**
  112. * 返回去年开始和结束的时间戳
  113. *
  114. * @return array
  115. */
  116. public static function lastYear()
  117. {
  118. $year = date('Y') - 1;
  119. return [
  120. mktime(0, 0, 0, 1, 1, $year),
  121. mktime(23, 59, 59, 12, 31, $year)
  122. ];
  123. }
  124. public static function dayOf()
  125. {
  126. }
  127. /**
  128. * 获取几天前零点到现在/昨日结束的时间戳
  129. *
  130. * @param int $day 天数
  131. * @param bool $now 返回现在或者昨天结束时间戳
  132. * @return array
  133. */
  134. public static function dayToNow($day = 1, $now = true)
  135. {
  136. $end = time();
  137. if (!$now) {
  138. list($foo, $end) = self::yesterday();
  139. }
  140. return [
  141. mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
  142. $end
  143. ];
  144. }
  145. /**
  146. * 返回几天前的时间戳
  147. *
  148. * @param int $day
  149. * @return int
  150. */
  151. public static function daysAgo($day = 1)
  152. {
  153. $nowTime = time();
  154. return $nowTime - self::daysToSecond($day);
  155. }
  156. /**
  157. * 返回几天后的时间戳
  158. *
  159. * @param int $day
  160. * @return int
  161. */
  162. public static function daysAfter($day = 1)
  163. {
  164. $nowTime = time();
  165. return $nowTime + self::daysToSecond($day);
  166. }
  167. /**
  168. * 天数转换成秒数
  169. *
  170. * @param int $day
  171. * @return int
  172. */
  173. public static function daysToSecond($day = 1)
  174. {
  175. return $day * 86400;
  176. }
  177. /**
  178. * 周数转换成秒数
  179. *
  180. * @param int $week
  181. * @return int
  182. */
  183. public static function weekToSecond($week = 1)
  184. {
  185. return self::daysToSecond() * 7 * $week;
  186. }
  187. private static function startTimeToEndTime()
  188. {
  189. }
  190. /**
  191. * 获取指定年月的开始和结束时间戳
  192. * @param int $y 年份
  193. * @param int $m 月份
  194. * @return array(开始时间,结束时间)
  195. */
  196. public static function time_start_end($y=0,$m=0)
  197. {
  198. $y = $y ? $y : date('Y');
  199. $m = $m ? $m : date('m');
  200. $d = date('t', strtotime($y.'-'.$m));
  201. return array("firsttime"=>strtotime($y.'-'.$m),"lasttime"=>mktime(23,59,59,$m,$d,$y));
  202. }
  203. /*
  204. 获取多少个工作日以后的日期
  205. $cost -int
  206. $start -int
  207. return string
  208. */
  209. public static function get_all_day($cost,$start='')
  210. {
  211. $workday = array(1,2,3,4,5);
  212. // 需要多少天
  213. $days = 0;
  214. if($start==''){
  215. $curday = time();
  216. }else{
  217. $curday = $start;
  218. }
  219. // 获取当前是星期几
  220. while($cost>=0){
  221. if(in_array(date('w',$curday),$workday)){ //是工作日
  222. $cost--;
  223. }
  224. $days++;
  225. if($cost>=0){
  226. $curday = mktime(date("H"),date("i"),date("s"),date('m',$curday),date('d',$curday)+1,date('Y',$curday));
  227. }
  228. }
  229. return date('Y-m-d H:i',$curday);
  230. }
  231. /*
  232. 获取当前时间戳毫秒
  233. */
  234. public static function msectime()
  235. {
  236. list($msec, $sec) = explode(' ', microtime());
  237. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  238. return $msectime;
  239. }
  240. /*
  241. 数据发布时间
  242. return string
  243. */
  244. public static function from_time($time)
  245. {
  246. $way = time() - (int)$time;
  247. if ($way < 60) {
  248. $r = '刚刚';
  249. } elseif ($way >= 60 && $way < 3600) {
  250. $r = floor($way / 60) . '分钟前';
  251. } elseif ($way >= 3600 && $way < 86400) {
  252. $r = floor($way / 3600) . '小时前';
  253. } elseif ($way >= 86400 && $way < 2592000) {
  254. $r = floor($way / 86400) . '天前';
  255. } elseif ($way >= 2592000 && $way < 15552000) {
  256. $r = floor($way / 2592000) . '个月前';
  257. } elseif ((int)$time == 0) {
  258. $r = '无';
  259. } else {
  260. $r = date('Y-m-d H:i', (int)$time);
  261. }
  262. return $r;
  263. }
  264. /*
  265. 获取多少天后
  266. return -string
  267. */
  268. public static function get_days_ago($time)
  269. {
  270. $time=is_string($time)?strtotime($time):$time;
  271. $way = $time - time();
  272. $a = date('Y-m-d', $time);
  273. $b = date('Y-m-d', time());
  274. $c = date('Y-m-d', time() + 24 * 3600);
  275. if ($way >= 86400 && $way < 2592000) {
  276. $r = floor($way / 86400) . '天后';
  277. } elseif ($way >= 2592000 && $way < 15552000) {
  278. $r = floor($way / 2592000) . '个月后';
  279. } elseif ($time < strtotime($b)) {
  280. $day = abs(ceil($way / 86400));
  281. if ($time == 0) {
  282. $r = "暂无";
  283. } elseif ($day < 90) {
  284. $r = '已过期' . $day . "天";
  285. } else {
  286. $r = "已过期3月+";
  287. }
  288. } elseif ($a == $b) {
  289. $r = '今天内';
  290. } elseif ($a == $c) {
  291. $r = '明天';
  292. } else {
  293. $r = date('Y-m-d', $time);
  294. }
  295. return $r;
  296. }
  297. /*
  298. 获取待办的时间
  299. return $str
  300. */
  301. public static function schdule_time($time)
  302. {
  303. $date = date("Y-m-d", $time);
  304. $today = date("Y-m-d");
  305. $tomorrow = date("Y-m-d", time() + 24 * 3600);
  306. $future = time() + 2 * 24 * 3600;
  307. if ($date == $today) {
  308. $str = "今天";
  309. } elseif ($date == $tomorrow) {
  310. $str = "明天";
  311. } elseif ($time >= $future) {
  312. $str = "未来几天";
  313. } else {
  314. $str = "已过期";
  315. }
  316. return $str;
  317. }
  318. /*
  319. 获取多少天
  320. return int
  321. */
  322. public static function days_num($time)
  323. {
  324. $way = time() - $time;
  325. return floor($way / 86400);
  326. }
  327. /*
  328. 获取多少小时
  329. return float
  330. */
  331. public static function hours_num($time)
  332. {
  333. return round($time / 60, 1);
  334. }
  335. /*
  336. 获取多少天后
  337. return string
  338. */
  339. public static function days_ago($daytime)
  340. {
  341. $daysago = ceil(($daytime - time()) / (3600 * 24));
  342. return $daysago . "天后";
  343. }
  344. /**
  345. * 求两个日期之间相差的天数
  346. * (针对1970年1月1日之后,求之前可以采用泰勒公式)
  347. * @param string $day1
  348. * @param string $day2
  349. * @return number
  350. */
  351. public static function between_two_days($second1, $second2)
  352. {
  353. if ($second1 < $second2) {
  354. $tmp = $second2;
  355. $second2 = $second1;
  356. $second1 = $tmp;
  357. }
  358. $day = floor(($second1 - $second2) / 86400);
  359. return $day;
  360. }
  361. /**
  362. * 获取本周所有日期
  363. * $time -起始日期
  364. * $format -输出格式
  365. * return array
  366. */
  367. public static function get_week($time = '', $format='Y-m-d')
  368. {
  369. $time = $time != '' ? $time : time();
  370. //获取当前周几
  371. $week = date('w', $time);
  372. $date = [];
  373. for ($i=1; $i<=7; $i++){
  374. $date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $time));
  375. }
  376. return $date;
  377. }
  378. /*
  379. 获取上周的开始时间和结束日期
  380. return array
  381. */
  382. public static function get_last_week()
  383. {
  384. $curr = date("Y-m-d");
  385. $w=date('w',time());//获取当前周的第几天 周日是 0 周一到周六是1-6
  386. $endTime=strtotime($curr.' -'.($w ? $w-1 : 6).' days');//获取本周开始日期,如果$w是0是周日:-6天;其它:$w-1天
  387. $startTime=strtotime(date('Y-m-d',strtotime(date('Y-m-d',$endTime)." -7 days")));
  388. return [$startTime,$endTime];
  389. }
  390. /**
  391. * 根据时间戳计算当月天数
  392. * @param
  393. */
  394. public static function getmonthdays($time)
  395. {
  396. $month = date('m', $time);
  397. $year = date('Y', $time);
  398. if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
  399. $days = '31';
  400. } elseif ($month == 2) {
  401. if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
  402. //判断是否是闰年
  403. $days = '29';
  404. } else {
  405. $days = '28';
  406. }
  407. } else {
  408. $days = '30';
  409. }
  410. return $days;
  411. }
  412. /**
  413. * 生成从开始时间到结束时间的日期数组
  414. * @param type,默认时间戳格式
  415. * @param type = 1 时,date格式
  416. * @param type = 2 时,获取每日开始、结束时间
  417. */
  418. public static function dateList($start, $end, $type = 0)
  419. {
  420. if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
  421. $i = 0;
  422. //从开始日期到结束日期的每日时间戳数组
  423. $d = array();
  424. if ($type == 1) {
  425. while ($start <= $end) {
  426. $d[$i] = date('Y-m-d', $start);
  427. $start = $start + 86400;
  428. $i++;
  429. }
  430. } else {
  431. while ($start <= $end) {
  432. $d[$i] = $start;
  433. $start = $start + 86400;
  434. $i++;
  435. }
  436. }
  437. if ($type == 2) {
  438. $list = array();
  439. foreach ($d as $k => $v) {
  440. $list[$k] = getDateRange($v);
  441. }
  442. return $list;
  443. } else {
  444. return $d;
  445. }
  446. }
  447. /**
  448. * 获取指定日期开始时间与结束时间
  449. */
  450. public static function getDateRange($timestamp)
  451. {
  452. $ret = array();
  453. $ret['sdate'] = strtotime(date('Y-m-d', $timestamp));
  454. $ret['edate'] = strtotime(date('Y-m-d', $timestamp)) + 86400;
  455. return $ret;
  456. }
  457. /**
  458. * 生成从开始月份到结束月份的月份数组
  459. * @param int $start 开始时间戳
  460. * @param int $end 结束时间戳
  461. */
  462. public static function monthList($start, $end)
  463. {
  464. if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
  465. $start = date('Y-m', $start);
  466. $end = date('Y-m', $end);
  467. //转为时间戳
  468. $start = strtotime($start . '-01');
  469. $end = strtotime($end . '-01');
  470. $i = 0;
  471. $d = array();
  472. while ($start <= $end) {
  473. //这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
  474. $d[$i] = $start;
  475. $start += strtotime('+1 month', $start) - $start;
  476. $i++;
  477. }
  478. return $d;
  479. }
  480. /**
  481. * 将秒数转换为时间 (年、天、小时、分、秒)
  482. * @param
  483. */
  484. public static function getTimeBySec($time)
  485. {
  486. $t='';
  487. if (is_numeric($time)) {
  488. $value = array(
  489. "years" => 0, "days" => 0, "hours" => 0,
  490. "minutes" => 0, "seconds" => 0,
  491. );
  492. if ($time >= 31556926) {
  493. $value["years"] = floor($time / 31556926);
  494. $time = ($time % 31556926);
  495. $t .= $value["years"] . "年";
  496. }
  497. if ($time >= 86400) {
  498. $value["days"] = floor($time / 86400);
  499. $time = ($time % 86400);
  500. $t .= $value["days"] . "天";
  501. }
  502. if ($time >= 3600) {
  503. $value["hours"] = floor($time / 3600);
  504. $time = ($time % 3600);
  505. $t .= $value["hours"] . "小时";
  506. }
  507. if ($time >= 60) {
  508. $value["minutes"] = floor($time / 60);
  509. $time = ($time % 60);
  510. $t .= $value["minutes"] . "分钟";
  511. }
  512. if ($time < 60) {
  513. $value["seconds"] = floor($time);
  514. $t .= $value["seconds"] . "秒";
  515. }
  516. return $t;
  517. } else {
  518. return (bool)FALSE;
  519. }
  520. }
  521. /**
  522. * 根据类型获取上一类型时间戳数组
  523. */
  524. public static function getLstTimeByType($type = 'today')
  525. {
  526. switch ($type) {
  527. case 'yesterday' :
  528. $timeArr = self::yesterday();
  529. break;
  530. case 'week' :
  531. $timeArr = self::week();
  532. break;
  533. case 'lastWeek' :
  534. $timeArr = self::lastWeek();
  535. break;
  536. case 'month' :
  537. $timeArr = self::month();
  538. break;
  539. case 'lastMonth' :
  540. $timeArr = self::lastMonth();
  541. break;
  542. case 'quarter' :
  543. //本季度
  544. $month = date('m');
  545. if ($month == 1 || $month == 2 || $month == 3) {
  546. $daterange_start_time = strtotime(date('Y-01-01 00:00:00'));
  547. $daterange_end_time = strtotime(date("Y-03-31 23:59:59"));
  548. } elseif ($month == 4 || $month == 5 || $month == 6) {
  549. $daterange_start_time = strtotime(date('Y-04-01 00:00:00'));
  550. $daterange_end_time = strtotime(date("Y-06-30 23:59:59"));
  551. } elseif ($month == 7 || $month == 8 || $month == 9) {
  552. $daterange_start_time = strtotime(date('Y-07-01 00:00:00'));
  553. $daterange_end_time = strtotime(date("Y-09-30 23:59:59"));
  554. } else {
  555. $daterange_start_time = strtotime(date('Y-10-01 00:00:00'));
  556. $daterange_end_time = strtotime(date("Y-12-31 23:59:59"));
  557. }
  558. $timeArr = array($daterange_start_time, $daterange_end_time);
  559. break;
  560. case 'lastQuarter' :
  561. //上季度
  562. $month = date('m');
  563. if ($month == 1 || $month == 2 || $month == 3) {
  564. $year = date('Y') - 1;
  565. $daterange_start_time = strtotime(date($year . '-10-01 00:00:00'));
  566. $daterange_end_time = strtotime(date($year . '-12-31 23:59:59'));
  567. } elseif ($month == 4 || $month == 5 || $month == 6) {
  568. $daterange_start_time = strtotime(date('Y-01-01 00:00:00'));
  569. $daterange_end_time = strtotime(date("Y-03-31 23:59:59"));
  570. } elseif ($month == 7 || $month == 8 || $month == 9) {
  571. $daterange_start_time = strtotime(date('Y-04-01 00:00:00'));
  572. $daterange_end_time = strtotime(date("Y-06-30 23:59:59"));
  573. } else {
  574. $daterange_start_time = strtotime(date('Y-07-01 00:00:00'));
  575. $daterange_end_time = strtotime(date("Y-09-30 23:59:59"));
  576. }
  577. $timeArr = array($daterange_start_time, $daterange_end_time);
  578. break;
  579. case 'year' :
  580. $timeArr = self::year();
  581. break;
  582. case 'lastYear' :
  583. $timeArr = self::lastYear();
  584. break;
  585. default :
  586. $timeArr = self::today();
  587. break;
  588. }
  589. return $timeArr;
  590. }
  591. /**
  592. * 根据类型获取开始结束时间戳数组
  593. * @param
  594. */
  595. public static function getTimeByType($type = 'today', $is_last = false)
  596. {
  597. $daterange_start_time_last_time='';
  598. $daterange_end_time_last_time='';
  599. $lastArr = [];
  600. switch ($type) {
  601. case 'yesterday' :
  602. $timeArr = self::yesterday();
  603. $lastArr = self::yesterday(1);
  604. break;
  605. case 'week' :
  606. $timeArr = self::week();
  607. $lastArr = self::lastWeek();
  608. break;
  609. case 'lastWeek' :
  610. $timeArr = self::lastWeek();
  611. $lastArr = self::lastWeek(1);
  612. break;
  613. case 'month' :
  614. $timeArr = self::month();
  615. $lastArr = self::lastMonth();
  616. break;
  617. case 'lastMonth' :
  618. $timeArr = self::lastMonth();
  619. $lastArr = self::lastMonth(1);
  620. break;
  621. case 'quarter' :
  622. //本季度
  623. $month = date('m');
  624. if ($month == 1 || $month == 2 || $month == 3) {
  625. $daterange_start_time = strtotime(date('Y-01-01 00:00:00'));
  626. $daterange_end_time = strtotime(date("Y-03-31 23:59:59"));
  627. } elseif ($month == 4 || $month == 5 || $month == 6) {
  628. $daterange_start_time = strtotime(date('Y-04-01 00:00:00'));
  629. $daterange_end_time = strtotime(date("Y-06-30 23:59:59"));
  630. } elseif ($month == 7 || $month == 8 || $month == 9) {
  631. $daterange_start_time = strtotime(date('Y-07-01 00:00:00'));
  632. $daterange_end_time = strtotime(date("Y-09-30 23:59:59"));
  633. } else {
  634. $daterange_start_time = strtotime(date('Y-10-01 00:00:00'));
  635. $daterange_end_time = strtotime(date("Y-12-31 23:59:59"));
  636. }
  637. //上季度
  638. $month = date('m');
  639. if ($month == 1 || $month == 2 || $month == 3) {
  640. $year = date('Y') - 1;
  641. $daterange_start_time_last_time = strtotime(date($year . '-10-01 00:00:00'));
  642. $daterange_end_time_last_time = strtotime(date($year . '-12-31 23:59:59'));
  643. } elseif ($month == 4 || $month == 5 || $month == 6) {
  644. $daterange_start_time_last_time = strtotime(date('Y-01-01 00:00:00'));
  645. $daterange_end_time_last_time = strtotime(date("Y-03-31 23:59:59"));
  646. } elseif ($month == 7 || $month == 8 || $month == 9) {
  647. $daterange_start_time_last_time = strtotime(date('Y-04-01 00:00:00'));
  648. $daterange_end_time_last_time = strtotime(date("Y-06-30 23:59:59"));
  649. } else {
  650. $daterange_start_time_last_time = strtotime(date('Y-07-01 00:00:00'));
  651. $daterange_end_time_last_time = strtotime(date("Y-09-30 23:59:59"));
  652. }
  653. $timeArr = array($daterange_start_time, $daterange_end_time);
  654. $lastArr = array($daterange_start_time_last_time, $daterange_end_time_last_time);
  655. break;
  656. case 'lastQuarter' :
  657. //上季度
  658. $month = date('m');
  659. if ($month == 1 || $month == 2 || $month == 3) {
  660. $year = date('Y') - 1;
  661. $daterange_start_time = strtotime(date($year . '-10-01 00:00:00'));
  662. $daterange_end_time = strtotime(date($year . '-12-31 23:59:59'));
  663. } elseif ($month == 4 || $month == 5 || $month == 6) {
  664. $daterange_start_time = strtotime(date('Y-01-01 00:00:00'));
  665. $daterange_end_time = strtotime(date("Y-03-31 23:59:59"));
  666. } elseif ($month == 7 || $month == 8 || $month == 9) {
  667. $daterange_start_time = strtotime(date('Y-04-01 00:00:00'));
  668. $daterange_end_time = strtotime(date("Y-06-30 23:59:59"));
  669. } else {
  670. $daterange_start_time = strtotime(date('Y-07-01 00:00:00'));
  671. $daterange_end_time = strtotime(date("Y-09-30 23:59:59"));
  672. }
  673. $timeArr = array($daterange_start_time, $daterange_end_time);
  674. $lastArr = array($daterange_start_time_last_time, $daterange_end_time_last_time);
  675. break;
  676. case 'year' :
  677. $timeArr = self::year();
  678. $lastArr = self::lastYear();
  679. break;
  680. case 'lastYear' :
  681. $timeArr = self::lastYear();
  682. $lastArr = self::lastYear(1);
  683. break;
  684. default :
  685. $timeArr = self::today();
  686. $lastArr = self::yesterday();
  687. break;
  688. }
  689. if ($is_last) {
  690. return $lastArr;
  691. } else {
  692. return $timeArr;
  693. }
  694. }
  695. /**
  696. * 图表时间范围处理,按月/天返回时间段数组
  697. *
  698. * @param int $start 开始时间(时间戳)
  699. * @param int $end 结束时间(时间戳)
  700. * @return array
  701. * @author Ymob
  702. * @datetime 2019-11-18 09:25:09
  703. */
  704. public static function getTimeArray($start = null, $end = null)
  705. {
  706. if ($start == null || $end == null) {
  707. $param = request()->param();
  708. switch ($param['type']) {
  709. // 本年
  710. case 'year':
  711. $start = strtotime(date('Y-01-01'));
  712. $end = strtotime('+1 year', $start) - 1;
  713. break;
  714. // 去年
  715. case 'lastYear':
  716. $start = strtotime(date(date('Y') - 1 . '-01-01'));
  717. $end = strtotime('+1 year', $start) - 1;
  718. break;
  719. // 本季度、上季度
  720. case 'quarter':
  721. case 'lastQuarter':
  722. $t = intval((date('m') - 1) / 3);
  723. $start_y = ($t * 3) + 1;
  724. $start = strtotime(date("Y-{$start_y}-01"));
  725. if ($param['type'] == 'lastQuarter') { // 上季度
  726. $start = strtotime('-3 month', $start);
  727. }
  728. $end = strtotime('+3 month', $start) - 1;
  729. break;
  730. // 本月、上月
  731. case 'month':
  732. case 'lastMonth':
  733. $start = strtotime(date('Y-m-01'));
  734. if ($param['type'] == 'lastMonth') {
  735. $start = strtotime('-1 month', $start);
  736. }
  737. $end = strtotime('+1 month', $start) - 1;
  738. break;
  739. // 本周、上周
  740. case 'week':
  741. case 'lastWeek':
  742. $start = strtotime('-' . (date('w') - 1) . 'day', strtotime(date('Y-m-d')));
  743. if ($param['type'] == 'lastWeek') {
  744. $start = strtotime('-7 day', $start);
  745. }
  746. $end = strtotime('+7 day', $start) - 1;
  747. break;
  748. // 今天、昨天
  749. case 'today':
  750. case 'yesterday':
  751. $start = strtotime(date('Y-m-d'));
  752. if ($param['type'] == 'yesterday') {
  753. $start = strtotime('-1 day', $start);
  754. }
  755. $end = strtotime('+1 day', $start) - 1;
  756. break;
  757. default:
  758. if ($param['start_time'] && $param['end_time']) {
  759. $start = $param['start_time'];
  760. $end = $param['end_time'];
  761. } else {
  762. // 本年
  763. $start = strtotime(date('Y-01-01'));
  764. $end = strtotime('+1 year', $start) - 1;
  765. }
  766. break;
  767. }
  768. }
  769. $between = [$start, $end];
  770. $list = [];
  771. $len = ($end - $start) / 86400;
  772. // 大于30天 按月统计、小于按天统计
  773. if ($len > 31) {
  774. $time_format = '%Y-%m';
  775. while (true) {
  776. $start = strtotime(date('Y-m-01', $start));
  777. $item = [];
  778. $item['type'] = date('Y-m', $start);
  779. $item['start_time'] = $start;
  780. $item['end_time'] = strtotime('+1 month', $start) - 1;
  781. $list[] = $item;
  782. if ($item['end_time'] >= $end) break;
  783. $start = $item['end_time'] + 1;
  784. }
  785. } else {
  786. $time_format = '%Y-%m-%d';
  787. while (true) {
  788. $item = [];
  789. $item['type'] = date('Y-m-d', $start);
  790. $item['start_time'] = $start;
  791. $item['end_time'] = strtotime('+1 day', $start) - 1;
  792. $list[] = $item;
  793. if ($item['end_time'] >= $end) break;
  794. $start = $item['end_time'] + 1;
  795. }
  796. }
  797. return [
  798. 'list' => $list, // 时间段列表
  799. 'time_format' => $time_format, // 时间格式 mysql 格式化时间戳
  800. 'between' => $between // 开始结束时间
  801. ];
  802. }
  803. /**
  804. * 简化时间的展示,可传入时间戳或者日期
  805. */
  806. public static function simpleTime($time){
  807. $time=is_string($time) ? strtotime($time) : $time;
  808. if($time==0){
  809. return "--";
  810. }
  811. $today=date("Y-m-d",$time);
  812. $year=date("Y",$time);
  813. if($today==date("Y-m-d",time())){
  814. return date('H:i',$time);
  815. }elseif($year==date("Y",time())){
  816. return date('m-d H:i',$time);
  817. }else{
  818. return date('Y-m-d H:i',$time);
  819. }
  820. }
  821. // 根据秒数获取时长
  822. public static function getDuration($seconds)
  823. {
  824. $hour = floor($seconds / 3600);
  825. $min = floor(($seconds % 3600) / 60);
  826. $sec = $seconds % 60;
  827. if ($hour > 0) {
  828. return $hour . "小时" . $min . "分" . $sec . "秒";
  829. } elseif ($min > 0) {
  830. return $min . "分" . $sec . "秒";
  831. } else {
  832. return $sec . "秒";
  833. }
  834. }
  835. }