Skip to content

DateHelper 日期常用助手函数

@package JoyceZ\LaravelLib\Helpers

今日起止

public static function today();

使用


DateHelper::today();

返回

array: [
  "start" => 1659283200,//2022-08-01 00:00:00
  "end" => 1659369599//2022-08-01 23:59:59
]

昨日起止

public static function yesterday();

使用


DateHelper::yesterday();

返回

array: [
  "start" => 1659196800,//2022-07-31 00:00:00
  "end" => 1659283199//2022-07-31 23:59:59
]

本周起止

public static function week();

使用


DateHelper::week();

返回

array: [
  "start" => 1659283200,//2022-08-01 00:00:00
  "end" => 1659887999//2022-08-07 23:59:59
]

上周起止

public static function lastWeek();

使用


DateHelper::lastWeek();

返回

array: [
  "start" => 1658678400,//2022-07-25 00:00:00
  "end" => 1659283199//2022-07-31 23:59:59
]

本月起止

public static function thisMonth();

使用


DateHelper::thisMonth();

返回

array: [
  "start" => 1659283200,//2022-08-01 00:00:00
  "end" => 1661961599//2022-08-31 23:59:59
]

上个月起止

public static function lastMonth();

使用


DateHelper::lastMonth();

返回

array: [
  "start" => 1656604800,//2022-07-01 00:00:00
  "end" => 1659283199//2022-07-31 23:59:59
]

几个月前月起止时间

/**
 * 几个月前
 *
 * @param int $month 月份
 * @return array
 */
public static function monthsAgo(int $month);

使用


DateHelper::monthsAgo(60);

返回

array: [
  "start" => 1501516800,//2017-08-01 00:00:00
  "end" => 1504195199//2017-08-31 23:59:59
]

某年

/**
 * 某年
 *
 * @param int $year 月份
 * @return array
 */
public static function someYear(int $year);

使用


DateHelper::someYear(60);

返回

array: [
  "start" => 1501516800,//2017-08-01 00:00:00
  "end" => 1504195199//2017-08-31 23:59:59
]

根据日期获取是星期几

/**
 * 
 * @param int $time 时间戳
 * @param string $format 格式前缀
 * @return mixed
 */
public static function getWeekName(int $time, $format = "")

使用


DateHelper::getWeekName(1659283200)

返回

string: 周一

当前时间多久之前

当前时间多久之前,以分钟到天核算

/**
 * 
 * @param int $curTime
 * @return string
 */
public static function formatTimeLongAgo(int $curTime);

使用


DateHelper::formatDateLongAgo(1659174224)

返回

string: 前天 17:43

现在到未来某天数日期段

/**
 * 获取指定开始日期到结束日期
 * @param int $start_day 开始天数,以当天开始:0,每增加一天,在当天基础上 +1
 * @param int $end_day
 * @return array
 */
public static function getFutureHowManyDays(int $start_day = 0, int $end_day = 7);

使用

获取未来7天日期。如果当前小时≥23点,那么就加1天,从明天开始;否则从今天开始。

应用场景:场馆订场/茶室预约,对预约数据进行筛选


DateHelper::getFutureHowManyDays(intval(date('H')) >= 23 ? 1 : 0, 7);

返回

array: [
  0 => "2022-08-01"
  1 => "2022-08-02"
  2 => "2022-08-03"
  3 => "2022-08-04"
  4 => "2022-08-05"
  5 => "2022-08-06"
  6 => "2022-08-07"
]

时间段范围

根据开始/结束时间,并以分钟为分界点生成 时间范围

/**
 * 
 * @param string $start_time 开始时间
 * @param string $end_time 结束时间
 * @param int $minute 间隔多少分钟
 * @return array
 */
public static function buildEveryDayTimeRange(string $start_time = '09:00', string $end_time = '22:30', int $minute = 30);

使用

应用场景:场馆订场/茶室预约。选择某天日期,展示当天可预约时间段。配合getFutureHowManyDays使用,可断定哪个时间段已被预约。


DateHelper::buildEveryDayTimeRange('09:00', date('H:i', time()));

返回

array: [
  1 => "09:00-09:30"
  2 => "09:30-10:00"
  3 => "10:00-10:30"
  4 => "10:30-11:00"
  5 => "11:00-11:30"
  6 => "11:30-12:00"
  7 => "12:00-12:30"
  8 => "12:30-13:00"
  9 => "13:00-13:30"
  10 => "13:30-14:00"
  11 => "14:00-14:30"
  12 => "14:30-15:00"
  13 => "15:00-15:30"
  14 => "15:30-16:00"
  15 => "16:00-16:30"
  16 => "16:30-17:00"
  17 => "17:00-17:30"
  18 => "17:30-18:00"
  19 => "18:00-18:30"
  20 => "18:30-19:00"
  21 => "19:00-19:30"
  22 => "19:30-20:00"
  23 => "20:00-20:30"
  24 => "20:30-21:00"
  25 => "21:00-21:30"
  26 => "21:30-22:00"
  27 => "22:00-22:30"
  28 => "22:30-23:00"
]

每周重复、隔周重复

根据开始日期至结束日期以及指定周几获得对应重复日期

/**
 * @param string $startDate 开始日期 Y-m-d
 * @param string $endDate 结束日期 Y-m-d
 * @param array $week 选中数字周几 [1,2,3,4,5,6,7] 或中文周几 ['周天', '周一', '周二', '周三', '周四', '周五', '周六']
 * @param bool $isApartWeek 是否隔周排期 true | false
 * @param bool $isNumWeek 是否是数字周几 true | false
 * @return array
 */
public static function generateDateWeek(string $startDate, string $endDate, array $week = [], $isApartWeek = false, $isNumWeek = true);

使用

应用场景:排课


DateHelper::generateDateWeek('2022-08-01', '2022-09-01', [2, 4, 5])

返回

array: [
  0 => "2022-08-02"
  1 => "2022-08-04"
  2 => "2022-08-05"
  3 => "2022-08-09"
  4 => "2022-08-11"
  5 => "2022-08-12"
  6 => "2022-08-16"
  7 => "2022-08-18"
  8 => "2022-08-19"
  9 => "2022-08-23"
  10 => "2022-08-25"
  11 => "2022-08-26"
  12 => "2022-08-30"
]

根据 MIT 许可证发布。