90) { return '很久前'; } elseif ($date >= 30 && $date <= 90) { return floor($date / 30) . '个月前'; } elseif ($date > 0 && $date < 30) { return $date . '天前'; } elseif ($hour < 24 && $hour > 0) { return $hour . '小时前'; } elseif ($minute < 60 && $minute > 0) { return $minute . '分钟前'; } elseif ($second < 60 && $second > 0) { return $second . '秒前'; } else { return '刚刚'; } } return "null"; } /** * 获取指定范围内的日期列表 * * @param $startTime string 起始日期 * @param $endTime string 结束日期 * @param $inclued bool 是否包含首尾日期,默认不含首尾日期 * @param $format string 返回的日期格式 * @param $interval string 日期的间隔 * @return array * @throws Exception */ public static function dateTimeList($startTime, $endTime, $inclued = false, $format = 'Y-m-d', $interval = 'tomorrow') { $starttime = strtotime($startTime); $endtime = strtotime($endTime); /** * 日期的间隔写法如下: * +1 week 3 days 7 hours 5 seconds 获取一周后+3天+7小时+5秒的时间戳 * -1 year 3 month 一年前-3个月 * +1 month 获取下个月最后一天:如果是1月31号计算得到的却是3月3号,应该是2月最后一天才对,改为last day of +1 month即可 * * Monday this week 这星期一 * Monday 是从今天开始的下一个星期一,如果今天是周一获取到的是这周一的日期,如果今天是周二到周日获取到的是下周一的日期 * last Monday 上星期一 (问题同上) 改为 date('m-d', strtotime('Monday this week'),time()-3600*24*7) 即可 * next Sunday 下星期日 (问题同上) * * yesterday 昨天 * first day of 获取本月1号的时间戳(永远为获取本月会死循环) * last day of 获取本月月底的时间戳 * first day of next month 下个月月初的时间戳 * last day of next month 或 last day of +1 month 下个月月底的时间戳 * last day of last month 上个月月底的时间戳 * 2022-02 first day of 获得固定时间的月初,得到2022-02-01 * 2022-02 last day of 得到2022-02-28 */ $i = 0; $arr = []; if ($inclued) { $arr[] = date($format, $starttime); } while (($starttime = strtotime($interval, $starttime)) < $endtime) { $arr[] = date($format, $starttime); if ($i++ > 500) { Log::error($arr); throw new Exception('循环超过限制');//防止死循环 } } if ($inclued) { $arr[] = date($format, $endtime); } return $arr; } /** * 拆分时段 * @param $start string 时间格式H:i:s * @param $end string 时间格式H:i:s * @param $count int 拆分个数 * @param $format string 返回的日期格式H:i:s * @return array */ public static function splitTimePeriod($start, $end, $count, $format = 'H:i:s') { $count = $count <= 0 ? 1 : $count; $second_total = strtotime($end) - strtotime($start); $arr = []; if ($count > 1) { $interval = floor($second_total / $count); $time_period = DateHelper::dateTimeList($start, $end, false, $format, "+ $interval seconds"); foreach ($time_period as $k => $period) { if ($k == 0) { $arr[] = $start . " - " . $period; } else { $arr[] = $time_period[$k - 1] . " - " . $period; } } $arr[] = end($time_period) . " - " . $end; } else { $arr[] = $start . " - " . $end; } return $arr; } }