Api接口实战:Carbon类获取上月第一天和最后一天
Carbon获取指定月份
tips1:本月第一天和本月最一天
Carbon::now()->startOfMonth()->toDateTimeString();
Carbon::now()->endOfMonth()->toDateTimeString();
tips2:上个月第一天
// 方法1
$firstOfMonth = new Carbon('first day of last month');
$firstOfMonth->startOfMonth()->toDateTimeString();
// 方法2
Carbon::now()->startOfMonth()->subMonth()->toDateTimeString();
// 方法3
Carbon::now()->subMonthWithoutOverflow()->startOfMonth()->toDateTimeString();
tips3:上个月最后一天
// 方法1
$lastOfMonth = new Carbon('last day of last month');
$lastOfMonth->endOfMonth()->toDateTimeString();
// 方法2
Carbon::now()->startOfMonth()->subMonth()->endOfMonth()->toDateTimeString();
// 方法3
Carbon::now()->subMonthWithoutOverflow()->endOfMonth()->toDateTimeString();
tips4:判断指定时间是否在本月内
Carbon::parse($timestamp)->isCurrentMonth()
tips5:判断指定时间是否在上个月内
Carbon::parse($timestamp)->isLastMonth()
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: