Carbon

参考文档地址

略,查看附录

略,查看附录

周、日

/**
 * Description: 日
 * Author: Shuxiaoyuan
 * Email: sxy@shuxiaoyuan.com
 * Website: https://www.shuxiaoyuan.com
 * DateTime: 2023/4/18 10:55
 *
 */
public function dayInfo()
{
    // 当前时间,设置时区
    $data['当前时间'] = Carbon::now();

    // 今天
    $data['今天'] = Carbon::today()->toDateString();

    // 昨天
    $data['昨天'] = Carbon::yesterday()->toDateString();

    $data['格式化1'] = Carbon::parse('1992-06-06 06:06:06')->toDateTimeString();
    $data['格式化2'] = Carbon::parse('1992-06-06 06:06:06')->format('Y-m-d H:i:s');

    // +n 天
    $data['今天+6天']  = Carbon::now()->addDays(6);
    $data['今天+15天'] = Carbon::now()->modify('+15 days');

    // -n 天
    $data['今天-6天'] = Carbon::now()->subDays(6);
    $data['今天-2天'] = Carbon::now()->modify('-2 days');

    // +n 周
    $data['今天+3周'] = Carbon::now()->addWeeks(3);

    // -n 周
    $data['今天-3周'] = Carbon::now()->subWeeks(3);

    // +n 小时
    $data['今天+25小时'] = Carbon::now()->addHours(25);

    // -n 小时
    $data['今天-2小时'] = Carbon::now()->subHours(2);

    // +n 分钟
    $data['今天+12分钟'] = Carbon::now()->addMinutes(12);

    // -n 分钟
    $data['今天-2分钟'] = Carbon::now()->subMinutes(2);

    // 获取指定天的开始时间,1992-06-06 00:00:00
    $data['指定天开始时间'] = Carbon::parse('19920606')->startOfDay()->toDateTimeString();

    // 获取指定天的结束时间,1992-06-06 23:59:59
    $data['指定天结束时间'] = Carbon::parse('1992-06-06')->endOfDay()->toDateTimeString();

    // 上周开始日期
    $data['上周开始日期'] = Carbon::now()->subWeek()->startOfWeek()->toDateString();

    // 上周结束日期
    $data['上周结束日期'] = Carbon::now()->subWeek()->endOfWeek()->toDateString();

    // 一年中的第几周
    $data['一年中的第几周'] = Carbon::now()->weekOfYear;

    // 周几 周一是1 周二是2 但是周日是0
    $data['周几'] = Carbon::now()->dayOfWeek;

    return Tools::outSuccessInfo($data);
}

附录1:格式化时间方法

方法名 参数 描述
__toString '' 按变量 $toStringFormat 的格式输出
toDateString '' 日期格式化输出
toTimeString '' 时间格式化输出
toDateTimeString '' 日期时间格式化输出
toDayDateTimeString '' 格式化输出如 "Fri, Jan 3, 2013 10:50 PM"
toAtomString '' 格式化输出如 "2012-10-20T14:12:26+00:00"
toCookieString '' 格式化输出如 "Friday, 02-Jan-2012 14:20:39 UTC"
toIso8601String '' 同 toAtomString
toRfc822String '' 格式化输出如 "Mon, 15 Aug 05 15:52:01 +0000"
toRfc850String '' 格式化输出如 "Monday, 15-Aug-05 15:52:01 UTC"
toRfc1036String '' 格式化输出如 "2005-08-15T15:52:01+0000"
toRfc1123String '' 格式化输出如 "Mon, 15 Aug 2005 15:52:01 +0000"
toRfc2822String '' 格式化输出如 "Mon, 15 Aug 05 15:52:01 +0000"
toRfc3339String '' 同 toAtomString
toRssString '' 格式化输出如 "Mon, 15 Aug 2005 15:52:01 +0000"
toW3cString '' 格式化输出如 "2005-08-15T15:52:01+00:00"
toFormattedDateString '' 格式化输出如 "Jan 11, 1999"
formatLocalized $format 指定格式本地化输出

附录2:时间判断

方法名 参数 描述
eq Carbon $dt 判断当前 Carbon 实例与指定 Carbon 对象时间是否一样
equalTo Carbon $dt 同 eq 方法
ne Carbon $dt 判断当前 Carbon 实例与指定 Carbon 对象时间是否不相同
notEqualTo Carbon $dt 同 ne 方法
gt Carbon $dt 判断当前 Carbon 实例是否大于指定 Carbon 对象时间
greaterThan Carbon $dt 同 gt 方法
gte Carbon $dt 判断当前 Carbon 实例是否大于等于指定 Carbon 对象时间
greaterThanOrEqualTo Carbon $dt 同 gte 方法
lt Carbon $dt 判断当前 Carbon 实例是否小于指定 Carbon 对象时间
lessThan Carbon $dt 同 lt 方法
lte Carbon $dt 判断当前 Carbon 实例是否小于等于指定 Carbon 对象时间
lessThanOrEqualTo Carbon $dt 同 lte 方法
between Carbon dt1, Carbondt2, $equal (true) 判断当前 Carbon 实例是否在指定 Carbon 对象时间之间, 第三个参数表示是否可以等于指定 Carbon 对象
min Carbon $dt 获取当前实例与指定 Carbon 对象中,最小的对象
minimum Carbon $dt 同min
max Carbon $dt 获取当前实例与指定 Carbon 对象中,最大的对象
maximum Carbon $dt 同max
closest Carbon dt1,Carbon dt2 获取最接近当前实例时间的 Carbon 对象
farthest Carbon dt1,Carbon dt2 获取最不接近当前实例时间的 Carbon 对象
isSameAs format,Carbondt 判断当前实例与指定 Carbon 对象的 format 格式化结果是否相同
isSameDay Carbon $dt 判断当前实例与指定 Carbon 对象是否是同一天
isSameMonth Carbon dt(null),ofSameYear (false) 判断当前实例与指定 Carbon 对象月份是否相同
isBirthday Carbon $dt 判断当前实例与指定 Carbon 对象月日数是否相同
isSameYear Carbon $dt 判断当前实例与指定 Carbon 对象年份是否相同
isSunday '' 判断当前实例是否是周日
isMonday '' 判断当前实例是否是周一
isTuesday '' 判断当前实例是否是周二
isWednesday '' 判断当前实例是否是周三
isThursday '' 判断当前实例是否是周四
isFriday '' 判断当前实例是否是周五
isSaturday '' 判断当前实例是否是周六
isYesterday '' 判断当前实例是否是昨天
isToday '' 判断当前实例是否是今天
isTomorrow '' 判断当前实例是否是明天
isWeekday '' 判断当前实例是否属于工作日
isWeekend '' 判断当前实例是否属于周末双休
isLastWeek '' 判断当前实例是否属于上周
isNextWeek '' 判断当前实例是否属于下一周
isLastMonth '' 判断当前实例是否属于上一个月
isCurrentMonth '' 判断当前实例是否属于当前月
isNextMonth '' 判断当前实例是否属于下一个月
isLastYear '' 判断当前实例是否属于去年
isCurrentYear '' 判断当前实例是否属于当前年
isNextYear '' 判断当前实例是否属于下一年
isLeapYear '' 判断当前实例是否属于闰年
isLongYear '' 判断当前实例是否属于长年,即一年不只有 52 个星期
isPast '' 判断当前实例是否属于过去
isFuture '' 判断当前实例是否属于未来

附录3:时间计算方法

方法名 参数 描述
addSecond $value(1) 当前实例添加指定数量的秒数,返回当前实例
subSecond $value(1) 当前实例减去指定数量的秒数,返回当前实例
addSeconds $value 同addSecond
subSeconds $value 同subSecond
addMinute $value(1) 当前实例添加指定数量的分钟数,返回当前实例
subMinute $value(1) 当前实例减去指定数量的分钟数,返回当前实例
addMinutes $value 同addMinute
subMinutes $value 同subMinute
addHour $value(1) 当前实例添加指定数量的小时数,返回当前实例
subHour $value(1) 当前实例减去指定数量的小时数,返回当前实例
addHours $value 同addHour
subHours $value 同subHour
addDay $value(1) 当前实例添加指定数量的天数,返回当前实例
subDay $value(1) 当前实例减去指定数量的天数,返回当前实例
addDays $value 同addDay
subDays $value 同subDay
addWeekday $value(1) 当前实例添加指定数量的工作日数,返回当前实例
subWeekday $value(1) 当前实例减去指定数量的工作日数,返回当前实例
addWeekdays $value 同addWeekday
subWeekdays $value 同subWeekday
addWeek $value(1) 当前实例添加指定数量的星期数,返回当前实例
subWeek $value(1) 当前实例减去指定数量的星期数,返回当前实例
addWeeks $value 同addWeek
subWeeks $value 同subWeek
addMonth $value(1) 当前实例添加指定数量的月数,返回当前实例
subMonth $value(1) 当前实例减去指定数量的月数,返回当前实例
addMonths $value 同addMonth
subMonths $value 同subMonth
addMonthWithOverflow(1) $value(1) 当前实例添加指定数量的月数(可溢出),返回当前实例
subMonthWithOverflow(1) $value(1) 当前实例添加指定数量的月数(可溢出),返回当前实例
addMonthsWithOverflow $value 同addMonthWithOverflow
subMonthsWithOverflow $value 同subMonthWithOverflow
addMonthNoOverflow(1) $value(1) 当前实例添加指定数量的月数(不可溢出),返回当前实例
subMonthNoOverflow(1) $value(1) 当前实例添加指定数量的月数(不可溢出),返回当前实例
addMonthsNoOverflow $value 同addMonthNoOverflow
subMonthsNoOverflow $value 同subMonthNoOverflow
addQuarter(1) $value(1) 当前实例添加指定数量的季度数,返回当前实例
subQuarter(1) $value(1) 当前实例减去指定数量的季度数,返回当前实例
addQuarters $value 同addQuarter
subQuarters $value 同subQuarter
addYear(1) $value(1) 当前实例添加指定数量的年数,返回当前实例
subYear(1) $value(1) 当前实例减去指定数量的年数,返回当前实例
addYears $value 同addYear
subYears $value 同subYear
addCentury(1) $value(1) 当前实例添加指定数量的世纪数,返回当前实例
subCentury(1) $value(1) 当前实例减去指定数量的世纪数,返回当前实例
addCenturies $value 同addCentury
subCenturies $value 同subCentury

附录4:时间差值比较方法

方法名 参数 描述
diffInSeconds Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的秒数差,前者 - 后者; abs表示是否返回绝对值
secondsSinceMidnight '' 获取当前实例时间的 0 时 0 分 0 秒 与当前实例时间的秒差
secondsUntilEndOfDay '' 获取当前实例时间的 23 时 59 分 59 秒 与当前实例时间的秒差
diffInMinutes Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的分钟差, 取整
diffInHours Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的小时差, 取整
diffInHoursFiltered Closure callback, Carbondt (null), $abs (true) 获取指定 Carbon 对象与当前实例时间的 (通过回调函数较验的) 小时差, 取整
diffInDays Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的天数差, 取整
diffInDaysFiltered Closure callback, Carbondt (null), $abs (true) 获取指定 Carbon 对象与当前实例时间的 (通过回调函数较验的) 天数差, 取整
diffInWeekdays Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的工作日数差, 取整
diffInWeekendDays Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的双休日数差, 取整
diffInWeeks Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的星期数差, 取整
diffInMonths Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的月数差, 取整
diffInYears Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的年数差, 取整
diffFiltered CarbonInterval ci, Closurecallback, Carbon dt (null),abs (true) 获取指定 Carbon 对象与当前实例时间的 (通过回调函数较验的)$ci差, 取整
diffForHumans Carbon other (null),absolute (false), $short (false) 获取指定 Carbon 对象与当前实例时间的时间差,以便于人类阅读的格式呈现

附录5:修改方法 Carbon 实例

方法名 参数 描述
setDate $year, $month, $day 设置当前实例的年,月,日
setDateTime $year, $month, $day, $hour, $minute, $second (0) 设置当前实例的年,月,日,时,分,秒
setTimeFromTimeString $time 根据 H:i:s 字符串设置当前实例时间
timestamp $value 根据时间戳设置当前实例时间
second $value 设置当前实例时间指定秒
minute $value 设置当前实例时间指定分钟
hour $value 设置当前实例时间指定小时
day $value 设置当前实例时间指定天
month $value 设置当前实例时间指月份
year $value 设置当前实例时间指定年份
startOfDay '' 重置当前实例时间为 0 时 0 分 0 秒
endOfDay '' 重置当前实例时间为 23 时 59 分 59 秒
startOfWeek '' 重置当前实例时间为 本周的第一天,同时设置 0 时 0 分 0 秒
endOfWeek '' 重置当前实例时间为 本周的最后一天,同时设置 23 时 59 分 59 秒
startOfMonth '' 重置当前实例时间为 本月第一天,同时设置 0 时 0 分 0 秒
endOfMonth '' 重置当前实例时间为 本月最后一天,同时设置 23 时 59 分 59 秒
startOfQuarter '' 重置当前实例时间为 本季度第一天,同时设置 0 时 0 分 0 秒
endOfQuarter '' 重置当前实例时间为 本季度最后一天,同时设置 23 时 59 分 59 秒
startOfYear '' 重置当前实例时间为 本年第一天,同时设置 0 时 0 分 0 秒
endOfYear '' 重置当前实例时间为 本年最后一天,同时设置 23 时 59 分 59 秒
startOfDecade '' 重置当前实例时间为 所在十年的第一天,同时设置 0 时 0 分 0 秒
endOfDecade '' 重置当前实例时间为 所在十年的最后一天,同时设置 23 时 59 分 59 秒
startOfCentury '' 重置当前实例时间为 本世纪的第一天,同时设置 0 时 0 分 0 秒
endOfCentury '' 重置当前实例时间为 本世纪的最后一天,同时设置 23 时 59 分 59 秒
next $dayOfWeek (null) 重置当前实例时间为 下一个指定dayOfWeek,同时设置 0 时 0 分 0 秒
previous $dayOfWeek (null) 重置当前实例时间为 上一个指定dayOfWeek,同时设置 0 时 0 分 0 秒
nextWeekday '' 重置当前实例时间为 下一个工作日,同时设置 0 时 0 分 0 秒
previousWeekday '' 重置当前实例时间为 上一个工作日,同时设置 0 时 0 分 0 秒
nextWeekendDay '' 重置当前实例时间为 下一个双休日,同时设置 0 时 0 分 0 秒
previousWeekendDay '' 重置当前实例时间为 上一个双休日,同时设置 0 时 0 分 0 秒
firstOfMonth $dayOfWeek (null) 重置当前实例时间为 本月第一周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
nthOfMonth nth,dayOfWeek 重置当前实例时间为 本月第 n 周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
lastOfMonth $dayOfWeek (null) 重置当前实例时间为 本月最后一周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
firstOfQuarter $dayOfWeek (null) 重置当前实例时间为 当前季度第一周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
nthOfQuarter nth,dayOfWeek 重置当前实例时间为 当前季度第 n 周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
lastOfQuarter $dayOfWeek (null) 重置当前实例时间为 当前季度最后一周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
firstOfYear $dayOfWeek (null) 重置当前实例时间为 本年第一周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
nthOfYear nth,dayOfWeek 重置当前实例时间为 本年第 n 周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
lastOfYear $dayOfWeek (null) 重置当前实例时间为 本年最后一周的指定dayOfWeek,同时设置 0 时 0 分 0 秒
average Carbon $dt 重置当前实例时间为 与指定 Carbon 对象的中间时刻
modify $modify 按 modify 字符串重置当前实例时间