Carbon mixin 用于处理工作日和开放时间
Carbon mixin to handle business days and opening hours Professionally supported nesbot/carbon is now available
composer require cmixin/business-timeFirst load the mixin in some global bootstrap place of your app:
…Business days methods are now available on any Carbon instance used anywhere later.
By enabling BusinessTime you automatically benefit on every holidays features of BusinessDay,
see cmixin/business-day
As soon as you set opening hours (using the second parameter of BusinessTime::enable(),
Carbon::setOpeningHours([...]) or $carbonDate->setOpeningHours([...])), you'll be able to retrieve opening hours
on any Carbon instance or statically ($carbonDate->getOpeningHours() or Carbon::getOpeningHours()) as an
instance of OpeningHours (spatie/opening-hours),
see spatie/opening-hours for complete list of features of this class.
Then with opening hours, you'll get the following methods directly available on Carbon instances:
By default, holidays has no particular opening hours, it will use the opening hours of the current day of week, but
you can use the 'holidaysAreClosed' => true option to close the business on every holiday that is not specified
otherwise in the 'exceptions' option. Else you can use a custom exception handler to link holidays or any dynamic
calculation as below:
…To work with multiple schedules or to use all the features without loading globally the mixin into Carbon,
you can create Schedule instances:
…Allows to know if the business is usually on open on a given day.
Carbon::isOpenOn('monday') // Returns true if there is at least 1 open range of
// hours set for Monday (in the regular schedule)
// Carbon::MONDAY would also works
$date->isOpenOn('monday') // Same as above but using local config of $date
Carbon::isOpenOn('2020-09-03') // Returns true if there is at least 1 open range of
// hours set for the date 2020-09-03 (considering both
// the regular schedule and the exceptions)Opposite of isOpenOn
Carbon::isClosedOn('monday')
Carbon::isClosedOn('2020-09-03')
$date->isClosedOn('monday')Allows to know if the business is usually on open at a given moment.
Carbon::isOpen() // returns true if the business is now open
$carbonDate->isOpen() // returns true if the business is open at the current date and time
if (Carbon::isOpen()) {
$closingTime = Carbon::nextClose()->isoFormat('LT');
echo "It's now open and until $closingTime.";
}Opposite of isOpen
Carbon::isClosed() // returns true if the business is now closed
$carbonDate->isClosed() // returns true if the business is closed at the current date and time
if (Carbon::isClosed()) {
$openingTime = Carbon::nextClose()->calendar();
echo "It's now closed and will re-open $openingTime.";
}Go to next open-business time.
Carbon::nextOpen() // go to next open time from now
$carbonDate->nextOpen() // go to next open time from $carbonDateGo to next closed-business time.
Carbon::nextClose() // go to next close time from now
$carbonDate->nextClose() // go to暂无开放 Issues,或尚未同步最近议题。