Dateset.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. declare (strict_types = 1);
  3. namespace dateset;
  4. /**
  5. * 日期时间处理类
  6. */
  7. class Dateset
  8. {
  9. const YEAR = 31536000;
  10. const MONTH = 2592000;
  11. const WEEK = 604800;
  12. const DAY = 86400;
  13. const HOUR = 3600;
  14. const MINUTE = 60;
  15. /**
  16. * 间隔时间段格式化
  17. * @param int $time 时间戳
  18. * @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】
  19. * @return string
  20. */
  21. function time_trans($time, $format = 'd')
  22. {
  23. $now = time();
  24. $diff = $now - $time;
  25. if ($diff < self::MINUTE) {
  26. return '1分钟前';
  27. } else if ($diff < self::HOUR) {
  28. return floor($diff / self::MINUTE) . '分钟前';
  29. } else if ($diff < self::DAY) {
  30. return floor($diff / self::HOUR) . '小时前';
  31. }
  32. $yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间
  33. $yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间
  34. $two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间
  35. $three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间
  36. $four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间
  37. $five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间
  38. $six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间
  39. $seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间
  40. if ($time > $yes_start_time && $time < $yes_end_time) {
  41. return '昨天';
  42. }
  43. if ($time > $yes_start_time && $time < $two_end_time) {
  44. return '1天前';
  45. }
  46. if ($time > $yes_start_time && $time < $three_end_time) {
  47. return '2天前';
  48. }
  49. if ($time > $yes_start_time && $time < $four_end_time) {
  50. return '3天前';
  51. }
  52. if ($time > $yes_start_time && $time < $five_end_time) {
  53. return '4天前';
  54. }
  55. if ($time > $yes_start_time && $time < $six_end_time) {
  56. return '5天前';
  57. }
  58. if ($time > $yes_start_time && $time < $seven_end_time) {
  59. return '6天前';
  60. }
  61. switch ($format) {
  62. case 'd':
  63. $show_time = date('Y-m-d', $time);
  64. break;
  65. case 'i':
  66. $show_time = date('Y-m-d H:i', $time);
  67. break;
  68. case 's':
  69. $show_time = date('Y-m-d H:i:s', $time);
  70. break;
  71. }
  72. return $show_time;
  73. }
  74. /**
  75. * 计算两个时间戳之间相差的时间
  76. *
  77. * $differ = self::differ(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
  78. * $differ = self::differ(60, 182, 'minutes'); // 2
  79. *
  80. * @param int $remote timestamp to find the span of
  81. * @param int $local timestamp to use as the baseline
  82. * @param string $output formatting string
  83. * @return string when only a single output is requested
  84. * @return array associative list of all outputs requested
  85. * @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php
  86. */
  87. public static function differ($remote, $local = null, $output = 'years,months,weeks,days,hours,minutes,seconds')
  88. {
  89. // Normalize output
  90. $output = trim(strtolower((string)$output));
  91. if (!$output) {
  92. // Invalid output
  93. return false;
  94. }
  95. // Array with the output formats
  96. $output = preg_split('/[^a-z]+/', $output);
  97. // Convert the list of outputs to an associative array
  98. $output = array_combine($output, array_fill(0, count($output), 0));
  99. // Make the output values into keys
  100. extract(array_flip($output), EXTR_SKIP);
  101. if ($local === null) {
  102. // Calculate the span from the current time
  103. $local = time();
  104. }
  105. // Calculate timespan (seconds)
  106. $timespan = abs($remote - $local);
  107. if (isset($output['years'])) {
  108. $timespan -= self::YEAR * ($output['years'] = (int)floor($timespan / self::YEAR));
  109. }
  110. if (isset($output['months'])) {
  111. $timespan -= self::MONTH * ($output['months'] = (int)floor($timespan / self::MONTH));
  112. }
  113. if (isset($output['weeks'])) {
  114. $timespan -= self::WEEK * ($output['weeks'] = (int)floor($timespan / self::WEEK));
  115. }
  116. if (isset($output['days'])) {
  117. $timespan -= self::DAY * ($output['days'] = (int)floor($timespan / self::DAY));
  118. }
  119. if (isset($output['hours'])) {
  120. $timespan -= self::HOUR * ($output['hours'] = (int)floor($timespan / self::HOUR));
  121. }
  122. if (isset($output['minutes'])) {
  123. $timespan -= self::MINUTE * ($output['minutes'] = (int)floor($timespan / self::MINUTE));
  124. }
  125. // Seconds ago, 1
  126. if (isset($output['seconds'])) {
  127. $output['seconds'] = $timespan;
  128. }
  129. if (count($output) === 1) {
  130. // Only a single output was requested, return it
  131. return array_pop($output);
  132. }
  133. // Return array
  134. return $output;
  135. }
  136. /**
  137. * 获取指定年月拥有的天数
  138. * @param int $month
  139. * @param int $year
  140. * @return false|int|string
  141. */
  142. public static function days_in_month($month, $year)
  143. {
  144. if (function_exists("cal_days_in_month")) {
  145. return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  146. } else {
  147. return date('t', mktime(0, 0, 0, $month, 1, $year));
  148. }
  149. }
  150. /**
  151. * 将秒数转换为时间 (小时、分、秒)
  152. * @param
  153. */
  154. function getTimeBySec($time,$second=true)
  155. {
  156. if (is_numeric($time)) {
  157. $value = array(
  158. "hours" => 0,
  159. "minutes" => 0, "seconds" => 0,
  160. );
  161. $t='';
  162. if ($time >= 3600) {
  163. $value["hours"] = floor($time / 3600);
  164. $time = ($time % 3600);
  165. $t .= $value["hours"] . "小时";
  166. }
  167. if ($time >= 60) {
  168. $value["minutes"] = floor($time / 60);
  169. $time = ($time % 60);
  170. $t .= $value["minutes"] . "分钟";
  171. }
  172. if ($time > 0 && $time < 60 && $second==true) {
  173. $value["seconds"] = floor($time);
  174. $t .= $value["seconds"] . "秒";
  175. }
  176. return $t;
  177. } else {
  178. return (bool)FALSE;
  179. }
  180. }
  181. /**
  182. * 将秒数转换为时间 (年、天、小时、分、秒)
  183. * @param
  184. */
  185. function getDateBySec($time,$second=false)
  186. {
  187. if (is_numeric($time)) {
  188. $value = array(
  189. "years" => 0, "days" => 0, "hours" => 0,
  190. "minutes" => 0, "seconds" => 0,
  191. );
  192. $t='';
  193. if ($time >= 31556926) {
  194. $value["years"] = floor($time / 31556926);
  195. $time = ($time % 31556926);
  196. $t .= $value["years"] . "年";
  197. }
  198. if ($time >= 86400) {
  199. $value["days"] = floor($time / 86400);
  200. $time = ($time % 86400);
  201. $t .= $value["days"] . "天";
  202. }
  203. if ($time >= 3600) {
  204. $value["hours"] = floor($time / 3600);
  205. $time = ($time % 3600);
  206. $t .= $value["hours"] . "小时";
  207. }
  208. if ($time >= 60) {
  209. $value["minutes"] = floor($time / 60);
  210. $time = ($time % 60);
  211. $t .= $value["minutes"] . "分钟";
  212. }
  213. if ($time < 60 && $second==true) {
  214. $value["seconds"] = floor($time);
  215. $t .= $value["seconds"] . "秒";
  216. }
  217. return $t;
  218. } else {
  219. return (bool)FALSE;
  220. }
  221. }
  222. /*
  223. *根据年月计算有几天
  224. */
  225. function getmonthByYM($param)
  226. {
  227. $month = $param['month'] ? $param['month'] : date('m', time());
  228. $year = $param['year'] ? $param['year'] : date('Y', time());
  229. if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
  230. $days = '31';
  231. } elseif ($month == 2) {
  232. if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
  233. //判断是否是闰年
  234. $days = '29';
  235. } else {
  236. $days = '28';
  237. }
  238. } else {
  239. $days = '30';
  240. }
  241. return $days;
  242. }
  243. /**
  244. * 根据时间戳计算当月天数
  245. * @param
  246. */
  247. function getmonthdays($time)
  248. {
  249. $month = date('m', $time);
  250. $year = date('Y', $time);
  251. if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
  252. $days = '31';
  253. } elseif ($month == 2) {
  254. if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
  255. //判断是否是闰年
  256. $days = '29';
  257. } else {
  258. $days = '28';
  259. }
  260. } else {
  261. $days = '30';
  262. }
  263. return $days;
  264. }
  265. /**
  266. * 生成从开始时间到结束时间的日期数组
  267. * @param type,默认时间戳格式
  268. * @param type = 1 时,date格式
  269. * @param type = 2 时,获取每日开始、结束时间
  270. */
  271. function dateList($start, $end, $type = 0)
  272. {
  273. if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
  274. $i = 0;
  275. //从开始日期到结束日期的每日时间戳数组
  276. $d = array();
  277. if ($type == 1) {
  278. while ($start <= $end) {
  279. $d[$i] = date('Y-m-d', $start);
  280. $start = $start + 86400;
  281. $i++;
  282. }
  283. } else {
  284. while ($start <= $end) {
  285. $d[$i] = $start;
  286. $start = $start + 86400;
  287. $i++;
  288. }
  289. }
  290. if ($type == 2) {
  291. $list = array();
  292. foreach ($d as $k => $v) {
  293. $list[$k] = $this->getDateRange($v);
  294. }
  295. return $list;
  296. } else {
  297. return $d;
  298. }
  299. }
  300. /**
  301. * 获取指定日期开始时间与结束时间
  302. */
  303. function getDateRange($timestamp)
  304. {
  305. $ret = array();
  306. $ret['sdate'] = strtotime(date('Y-m-d', $timestamp));
  307. $ret['edate'] = strtotime(date('Y-m-d', $timestamp)) + 86400;
  308. return $ret;
  309. }
  310. /**
  311. * 生成从开始月份到结束月份的月份数组
  312. * @param int $start 开始时间戳
  313. * @param int $end 结束时间戳
  314. */
  315. function monthList($start, $end)
  316. {
  317. if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
  318. $start = date('Y-m', $start);
  319. $end = date('Y-m', $end);
  320. //转为时间戳
  321. $start = strtotime($start . '-01');
  322. $end = strtotime($end . '-01');
  323. $i = 0;
  324. $d = array();
  325. while ($start <= $end) {
  326. //这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
  327. $d[$i] = $start;
  328. $start += strtotime('+1 month', $start) - $start;
  329. $i++;
  330. }
  331. return $d;
  332. }
  333. /**
  334. * 等于(时间段)数据处理
  335. *
  336. * @param $type
  337. * @return array
  338. * @since 2021-06-11
  339. * @author fanqi
  340. */
  341. function advancedDate($type)
  342. {
  343. // 本年度
  344. if ($type == 'year') {
  345. $arrTime = DataTime::year();
  346. $start_time = date('Y-m-d 00:00:00', $arrTime[0]);
  347. $end_time = date('Y-m-d 23:59:59', $arrTime[1]);
  348. }
  349. // 上一年度
  350. if ($type == 'lastYear') {
  351. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 year'));
  352. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 year'));
  353. }
  354. // 下一年度
  355. if ($type == 'nextYear') {
  356. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 year'));
  357. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 year'));
  358. }
  359. // 上半年
  360. if ($type == 'firstHalfYear') {
  361. $start_time = date('Y-01-01 00:00:00');
  362. $end_time = date('Y-06-30 23:59:59');
  363. }
  364. // 下半年
  365. if ($type == 'nextHalfYear') {
  366. $start_time = date('Y-07-01 00:00:00');
  367. $end_time = date('Y-12-31 23:59:59');
  368. }
  369. // 本季度
  370. if ($type == 'quarter') {
  371. $season = ceil((date('n')) / 3);
  372. $start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
  373. $end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
  374. }
  375. // 上一季度
  376. if ($type == 'lastQuarter') {
  377. $season = ceil((date('n')) / 3) - 1;
  378. $start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
  379. $end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
  380. }
  381. // 下一季度
  382. if ($type == 'nextQuarter') {
  383. $season = ceil((date('n')) / 3);
  384. $start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 + 1, 1, date('Y')));
  385. $end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3 + 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
  386. }
  387. // 本月
  388. if ($type == 'month') {
  389. $start_time = date('Y-m-01 00:00:00');
  390. $end_time = date('Y-m-31 23:59:59');
  391. }
  392. // 上月
  393. if ($type == 'lastMonth') {
  394. $start_time = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '-1 month'));
  395. $end_time = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '-1 month'));
  396. }
  397. // 下月
  398. if ($type == 'nextMonth') {
  399. $start_time = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '+1 month'));
  400. $end_time = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '+1 month'));
  401. }
  402. // 本周
  403. if ($type == 'week') {
  404. $start_time = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')));
  405. $end_time = date('Y-m-d 23:59:59', mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y')));
  406. }
  407. // 上周
  408. if ($type == 'lastWeek') {
  409. $date = date("Y-m-d");
  410. $w = date("w", strtotime($date));
  411. $d = $w ? $w - 1 : 6;
  412. $start = date("Y-m-d", strtotime($date . " - " . $d . " days"));
  413. $start_time = date('Y-m-d', strtotime($start . " - 7 days"));
  414. $end_time = date('Y-m-d', strtotime($start . " - 1 days"));
  415. }
  416. // 下周
  417. if ($type == 'nextWeek') {
  418. $date = date("Y-m-d");
  419. $w = date("w", strtotime($date));
  420. $d = $w ? $w - 1 : 6;
  421. $start = date("Y-m-d", strtotime($date . " - " . $d . " days"));
  422. $start_time = date('Y-m-d', strtotime($start . " + 7 days"));
  423. $end_time = date('Y-m-d', strtotime($start . " + 13 days"));
  424. }
  425. // 今天
  426. if ($type == 'today') {
  427. $start_time = date('Y-m-d 00:00:00');
  428. $end_time = date('Y-m-d 23:59:59');
  429. }
  430. // 昨天
  431. if ($type == 'yesterday') {
  432. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 day'));
  433. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
  434. }
  435. // 明天
  436. if ($type == 'tomorrow') {
  437. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
  438. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 day'));
  439. }
  440. // 过去3天
  441. if ($type == 'previous3day') {
  442. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-3 day'));
  443. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
  444. }
  445. // 过去5天
  446. if ($type == 'previous5day') {
  447. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-5 day'));
  448. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
  449. }
  450. // 过去7天
  451. if ($type == 'previous7day') {
  452. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-7 day'));
  453. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
  454. }
  455. // 过去10天
  456. if ($type == 'previous10day') {
  457. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-10 day'));
  458. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
  459. }
  460. // 过去30天
  461. if ($type == 'previous30day') {
  462. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-30 day'));
  463. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
  464. }
  465. // 未来3天
  466. if ($type == 'future3day') {
  467. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
  468. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+3 day'));
  469. }
  470. // 未来5天
  471. if ($type == 'future5day') {
  472. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
  473. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+5 day'));
  474. }
  475. // 未来7天
  476. if ($type == 'future7day') {
  477. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
  478. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+7 day'));
  479. }
  480. // 未来10天
  481. if ($type == 'future10day') {
  482. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
  483. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+10 day'));
  484. }
  485. // 未来30天
  486. if ($type == 'future30day') {
  487. $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
  488. $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+30 day'));
  489. }
  490. return [$start_time,$end_time];
  491. }
  492. /**
  493. * 根据时间戳获取星期几
  494. * @param $time 要转换的时间戳
  495. */
  496. function getTimeWeek($time, $i = 0)
  497. {
  498. $weekarray = array("日", "一", "二", "三", "四", "五", "六");
  499. $oneD = 24 * 60 * 60;
  500. return "星期" . $weekarray[date("w", $time + $oneD * $i)];
  501. }
  502. }