DateHelper.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/8/10
  6. * Time: 19:23
  7. */
  8. namespace time;
  9. use think\Exception;
  10. use think\Log;
  11. class DateHelper
  12. {
  13. /**
  14. * 检测是否为日期
  15. * @param $dateString
  16. * @return bool
  17. */
  18. public static function isDate($dateString)
  19. {
  20. /*date函数会给月和日补零,所以最终用unix时间戳来校验*/
  21. return strtotime(date('Y-m', strtotime($dateString))) === strtotime($dateString);
  22. }
  23. /**
  24. * 检测是否为Y-m-d格式日期
  25. * @param $date
  26. * @return bool
  27. */
  28. public static function isDateYmd($date)
  29. {
  30. if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) {
  31. return checkdate($parts[2], $parts[3], $parts[1]);
  32. } else {
  33. return false;
  34. }
  35. }
  36. /**
  37. * 日期格式转换
  38. * @param $dateTime string 原始日期
  39. * @param string $format 指定日期格式
  40. * @return false|string
  41. */
  42. public static function dateFormat($dateTime, $format = 'Y-m-d H:i:s')
  43. {
  44. $nowtime = strtotime($dateTime);//得到时间戳
  45. return date($format, $nowtime);//时间戳转日期
  46. }
  47. /**
  48. * 时间文字简化
  49. * @param $startdate [日期或时间戳]
  50. * @return string
  51. */
  52. public static function timeTip($startdate)
  53. {
  54. if ($startdate) {
  55. if (is_numeric($startdate)) {
  56. $startdate = date('Y-m-d H:i:s', $startdate);//时间戳转日期(要是日期的话可以不用转)
  57. }
  58. $enddate = date('Y-m-d H:i:s');//当前日期
  59. $date = floor((strtotime($enddate) - strtotime($startdate)) / 86400);
  60. $hour = floor((strtotime($enddate) - strtotime($startdate)) % 86400 / 3600);
  61. $minute = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 3600 / 60);
  62. $second = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 60);
  63. if ($date > 90) {
  64. return '很久前';
  65. } elseif ($date >= 30 && $date <= 90) {
  66. return floor($date / 30) . '个月前';
  67. } elseif ($date > 0 && $date < 30) {
  68. return $date . '天前';
  69. } elseif ($hour < 24 && $hour > 0) {
  70. return $hour . '小时前';
  71. } elseif ($minute < 60 && $minute > 0) {
  72. return $minute . '分钟前';
  73. } elseif ($second < 60 && $second > 0) {
  74. return $second . '秒前';
  75. } else {
  76. return '刚刚';
  77. }
  78. }
  79. return "null";
  80. }
  81. /**
  82. * 获取指定范围内的日期列表
  83. *
  84. * @param $startTime string 起始日期
  85. * @param $endTime string 结束日期
  86. * @param $inclued bool 是否包含首尾日期,默认不含首尾日期
  87. * @param $format string 返回的日期格式
  88. * @param $interval string 日期的间隔
  89. * @return array
  90. * @throws Exception
  91. */
  92. public static function dateTimeList($startTime, $endTime, $inclued = false, $format = 'Y-m-d', $interval = 'tomorrow')
  93. {
  94. $starttime = strtotime($startTime);
  95. $endtime = strtotime($endTime);
  96. /**
  97. * 日期的间隔写法如下:
  98. * +1 week 3 days 7 hours 5 seconds 获取一周后+3天+7小时+5秒的时间戳
  99. * -1 year 3 month 一年前-3个月
  100. * +1 month 获取下个月最后一天:如果是1月31号计算得到的却是3月3号,应该是2月最后一天才对,改为last day of +1 month即可
  101. *
  102. * Monday this week 这星期一
  103. * Monday 是从今天开始的下一个星期一,如果今天是周一获取到的是这周一的日期,如果今天是周二到周日获取到的是下周一的日期
  104. * last Monday 上星期一 (问题同上) 改为 date('m-d', strtotime('Monday this week'),time()-3600*24*7) 即可
  105. * next Sunday 下星期日 (问题同上)
  106. *
  107. * yesterday 昨天
  108. * first day of 获取本月1号的时间戳(永远为获取本月会死循环)
  109. * last day of 获取本月月底的时间戳
  110. * first day of next month 下个月月初的时间戳
  111. * last day of next month 或 last day of +1 month 下个月月底的时间戳
  112. * last day of last month 上个月月底的时间戳
  113. * 2022-02 first day of 获得固定时间的月初,得到2022-02-01
  114. * 2022-02 last day of 得到2022-02-28
  115. */
  116. $i = 0;
  117. $arr = [];
  118. if ($inclued) {
  119. $arr[] = date($format, $starttime);
  120. }
  121. while (($starttime = strtotime($interval, $starttime)) < $endtime) {
  122. $arr[] = date($format, $starttime);
  123. if ($i++ > 500) {
  124. Log::error($arr);
  125. throw new Exception('循环超过限制');//防止死循环
  126. }
  127. }
  128. if ($inclued) {
  129. $arr[] = date($format, $endtime);
  130. }
  131. return $arr;
  132. }
  133. /**
  134. * 拆分时段
  135. * @param $start string 时间格式H:i:s
  136. * @param $end string 时间格式H:i:s
  137. * @param $count int 拆分个数
  138. * @param $format string 返回的日期格式H:i:s
  139. * @return array
  140. */
  141. public static function splitTimePeriod($start, $end, $count, $format = 'H:i:s')
  142. {
  143. $count = $count <= 0 ? 1 : $count;
  144. $second_total = strtotime($end) - strtotime($start);
  145. $arr = [];
  146. if ($count > 1) {
  147. $interval = floor($second_total / $count);
  148. $time_period = DateHelper::dateTimeList($start, $end, false, $format, "+ $interval seconds");
  149. foreach ($time_period as $k => $period) {
  150. if ($k == 0) {
  151. $arr[] = $start . " - " . $period;
  152. } else {
  153. $arr[] = $time_period[$k - 1] . " - " . $period;
  154. }
  155. }
  156. $arr[] = end($time_period) . " - " . $end;
  157. } else {
  158. $arr[] = $start . " - " . $end;
  159. }
  160. return $arr;
  161. }
  162. }