Download the PHP package cmixin/business-time without Composer
On this page you can find all versions of the php package cmixin/business-time. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cmixin/business-time
More information about cmixin/business-time
Files in cmixin/business-time
Package business-time
Short Description Carbon mixin to handle business days and opening hours
License MIT
Informations about the package business-time
business-time
Carbon mixin to handle business days and opening hours
Professionally supported nesbot/carbon is now available
Install
Usage
First load the mixin in some global bootstrap place of your app:
Try in the live editor%3B%0A%0A%24date%20%3D%20Carbon%3A%3Aparse('2019-04-01%2017%3A25')%3B%0A%0Avar_dump(%24date-%3EisOpen())%3B%0Aecho%20%24date-%3EnextClose().%22%5Cn%22%3B%0A%0A%24date%20%3D%20Carbon%3A%3Aparse('2019-04-01%2018%3A25')%3B%0A%0Avar_dump(%24date-%3EisOpen())%3B%0Aecho%20%24date-%3EnextOpen().%22%5Cn%22%3B%0A%0A%2F%2F%20Exception%20for%20Christmas%0A%24date%20%3D%20Carbon%3A%3Aparse('2019-12-25%2017%3A25')%3B%0A%0Avar_dump(%24date-%3EisOpen())%3B%0A%0A%24date%20%3D%20Carbon%3A%3Aparse('2019-12-25%2011%3A25')%3B%0A%0Avar_dump(%24date-%3EisOpen())%3B%0A%0A%24date%20%3D%20Carbon%3A%3Aparse('2018-04-07%2011%3A25')%3B%0A%0Avar_dump(%24date-%3EisBusinessOpen())%3B%20%2F%2F%20use%20isBusinessOpen%20to%20consider%20holidays%20as%20closed%20all%20day%20long)
Business days methods are now available on any Carbon instance used anywhere later.
Features
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:
Holidays
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:
Try in the live editor%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20Or%20use%20-%3EisObservedHoliday()%20and%20set%20observed%20holidays%3A%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20https%3A%2F%2Fgithub.com%2Fkylekatarnls%2Fbusiness-day%23setobservedholidayszone%0D%0A%20%20%20%20%20%20%20%20switch%20(%24date-%3EgetHolidayId())%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20the%20ID%20%22christmas%22%20exists%20in%20the%20selected%20holidays%20region%20and%20matches%20the%20current%20date%3A%0D%0A%20%20%20%20%20%20%20%20%20%20case%20%27christmas%27%3A%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%5B%2710%3A00-12%3A00%27%5D%3B%0D%0A%20%20%20%20%20%20%20%20%20%20default%3A%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%5B%5D%3B%20%2F%2F%20All%20other%20holidays%20are%20closed%20all%20day%20long%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Here%20you%20can%20also%20pass%20context%20data%3A%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27hours%27%20%3D%3E%20%5B%5D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27data%27%20%20%3D%3E%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27reason%27%20%3D%3E%20%27Today%20is%20%27%20.%20%24date-%3EgetHolidayName()%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%2F%2F%20Else%2C%20typical%20day%20%3D%3E%20use%20days%20of%20week%20settings%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%5D%2C%0D%0A%5D)%3B%0D%0A%0D%0ACarbon%3A%3AsetHolidaysRegion(%27us-national%27)%3B%0D%0A%0D%0Avar_dump(Carbon%3A%3Aparse(%272018-12-25%2011%3A00%27)-%3EisOpen())%3B%20%2F%2F%20true%20%20matches%20custom%20opening%20hours%20of%20Christmas%0D%0Avar_dump(Carbon%3A%3Aparse(%272018-12-25%2013%3A00%27)-%3EisOpen())%3B%20%2F%2F%20false%0D%0Avar_dump(Carbon%3A%3Aparse(%272019-01-01%2011%3A00%27)-%3EisOpen())%3B%20%2F%2F%20false%20closed%20all%20day%20long%0D%0Avar_dump(Carbon%3A%3Aparse(%272019-01-02%2011%3A00%27)-%3EisOpen())%3B%20%2F%2F%20true%20%20not%20an%20holiday%20in%20us-national%20region%2C%20so%20it%27s%20open%20as%20any%20common%20wednesday)
Multiple business-hours
To work with multiple schedules or to use all the features without loading globally the mixin into Carbon
,
you can create Schedule
instances:
Try in the live editor%3B%0Avar_dump(%24fr-%3EsubOpenHours(%24d%2C%201)-%3Eformat(%27Y-m-d%20H%3Ai%3As%27))%3B%0A%0A%24d%20%3D%20CarbonImmutable%3A%3Aparse(%272022-10-20%2017%3A30%3A00%27)%3B%0Avar_dump(%24us-%3EisOpen(%24d))%3B%0Avar_dump(%24fr-%3EisOpen(%24d))%3B)
isOpenOn
Allows to know if the business is usually on open on a given day.
isClosedOn
Opposite of isOpenOn
isOpen
Allows to know if the business is usually on open at a given moment.
isClosed
Opposite of isOpen
nextOpen
Go to next open-business time.
nextClose
Go to next closed-business time.
previousOpen
Go to previous open-business time.
previousClose
Go to previous closed-business time.
addOpenTime
Add the given interval of time taking only into account open ranges of hours.
For instance, if the current day has ["09:00-12:00", "13:30-17:00"]
open range
of hours, adding 2 open hours when it's 11am will actually add 3 hours and 30
minutes (step over the midday break: an hour and a half) and so set the time to
14:30.
addOpenHours
Add the given number of hours taking only into account open ranges of hours.
addOpenMinutes
Add the given number of minutes taking only into account open ranges of hours.
addClosedTime
Add the given interval of time taking only into account closed ranges of hours.
addClosedHours
Add the given number of hours taking only into account closed ranges of hours.
addClosedMinutes
Add the given number of minutes taking only into account closed ranges of hours.
subOpenTime
Subtract the given interval of time taking only into account open ranges of hours.
subOpenHours
Subtract the given number of hours taking only into account open ranges of hours.
subOpenMinutes
Subtract the given number of minutes taking only into account open ranges of hours.
subClosedTime
Subtract the given interval of time taking only into account closed ranges of hours.
subClosedHours
Subtract the given number of hours taking only into account closed ranges of hours.
subClosedMinutes
Subtract the given number of minutes taking only into account closed ranges of hours.
getCurrentDayOpeningHours
Returns the opening hours current day settings (first matching exception or else current weekday settings).
isBusinessOpen / isOpenExcludingHolidays
Equivalent to isOpen
when 'holidaysAreClosed'
is set to true
.
Allows to know if the business is usually on open at a given moment and not an holidays. But you also can handle holidays with a dedicated exception for a finest setting. See Holidays section
isBusinessClosed / isClosedIncludingHolidays
Equivalent to isClosed
when 'holidaysAreClosed'
is set to true
.
Opposite of isOpenExcludingHolidays
nextBusinessOpen / nextOpenExcludingHolidays
Equivalent to nextOpen
when 'holidaysAreClosed'
is set to true
.
Go to next open time (considering all holidays as closed time). But prefer to handle holidays with a dedicated exception for a finest setting. See Holidays section
nextBusinessClose / nextCloseIncludingHolidays
Equivalent to nextClose
when 'holidaysAreClosed'
is set to true
.
Go to next closed time (considering all holidays as closed time). But prefer to handle holidays with a dedicated exception for a finest setting. See Holidays section
previousBusinessOpen / previousOpenExcludingHolidays
Equivalent to previousOpen
when 'holidaysAreClosed'
is set to true
.
Go to previous open time (considering all holidays as closed time). But prefer to handle holidays with a dedicated exception for a finest setting. See Holidays section
previousBusinessClose / previousCloseIncludingHolidays
Equivalent to previousClose
when 'holidaysAreClosed'
is set to true
.
Go to previous closed time (considering all holidays as closed time). But prefer to handle holidays with a dedicated exception for a finest setting. See Holidays section
currentOr*
Methods starting with currentOr
are followed by:
- a time-direction:
Next
/Previous
- optionally
Business
(meaning holidays are automatically considered as closed no matter the'holidaysAreClosed'
is true or false) - a state
Open
/Close
All currentOr*
methods return the current date-time if it's in the state
(see above), else they return the first date-time (next or previous according
to the given time-direction) where a state change to be the chosen state
(open / close).
Note: BusinessOpen
can also be written explicitly as OpenExcludingHolidays
and BusinessClose
as CloseIncludingHolidays
.
openOr*
Methods starting with openOr
are followed by:
- a time-direction:
Next
/Previous
- optionally
Business
(meaning holidays are automatically considered as closed no matter the'holidaysAreClosed'
is true or false) Close
(for open-or-next/previous-open, see currentOr*)
All openOr*
methods return the current date-time if it's open, else
they return the first date-time (next or previous according
to the given time-direction) the business close.
Note: BusinessClose
can also be written explicitly as CloseIncludingHolidays
.
closedOr*
Methods starting with closedOr
are followed by:
- a time-direction:
Next
/Previous
- optionally
Business
(meaning holidays are automatically considered as closed no matter the'holidaysAreClosed'
is true or false) Open
(for closed-or-next/previous-closed, see currentOr*)
All closedOr*
methods return the current date-time if it's closed, else
they return the first date-time (next or previous according
to the given time-direction) the business open.
Note: BusinessOpen
can also be written explicitly as OpenExcludingHolidays
.
diffAsBusinessInterval
Return open/closed interval of time between 2 dates/times.
The returned $interval
is an instance of CarbonInterval
. See https://carbon.nesbot.com/docs/#api-interval
Options are piped flags among:
BusinessTime::CLOSED_TIME
: return the interval of for closed time, return open time elseBusinessTime::RELATIVE_DIFF
: return negative value if start is before endBusinessTime::HOLIDAYS_ARE_CLOSED
: automatically consider holidays as closedBusinessTime::USE_DAYLIGHT_SAVING_TIME
: use DST native PHP diff result instead of real time (timestamp)
Examples:
diffInBusinessUnit
Return open/closed time in the given unit between 2 dates/times.
The first parameter is the unit singular or plural, in any case. The 2 other parameters are
the same as in diffAsBusinessInterval
diffInBusinessHours
Return open/closed number of hours (as a floating number) in the given unit between 2 dates/times.
The 2 parameters are
the same as in diffAsBusinessInterval
diffInBusinessMinutes
Return open/closed number of minutes (as a floating number) in the given unit between 2 dates/times.
The 2 parameters are
the same as in diffAsBusinessInterval
diffInBusinessSeconds
Return open/closed number of seconds (as a floating number) in the given unit between 2 dates/times.
The 2 parameters are
the same as in diffAsBusinessInterval
getCurrentOpenTimeRanges
Get list of ranges that contain the current date-time.
getCurrentOpenTimeRange
Get the first range that contain the current date-time.
getCurrentOpenTimePeriod
Get the first range that contain the current date-time as a CarbonPeriod
.
While getCurrentOpenTimeRange()
return a TimeRange
instance which only handle the time part, getCurrentOpenTimePeriod()
return
a CarbonPeriod
instance so you get whole date and time of the start and end.
This is mostly useful when having ranges overflowing midnight:
getCurrentOpenTimeRangeStart
Get the start of the current open time range (if open, holidays ignored).
getCurrentOpenTimeRangeEnd
Get the end of the current open time range (if open, holidays ignored).
getCurrentBusinessTimeRangeStart
Get the start of the current open time range (if open and not holiday).
getCurrentBusinessTimeRangeEnd
Get the end of the current open time range (if open and not holiday).
Laravel
To enable business-time globally in Laravel, set default openning hours and holidays settings in the config file config/carbon.php (create this file if it does not exist yet):
If you use Laravel but don't plan to use this global config to enable business-time, you may remove it from auto-discovery using:
Note about timezones
When you set an holidays region, it does not change the timezone, so if January 1st is an holiday,
->isHoliday()
returns true
from Carbon::parse('2010-01-01 00:00:00.000000)
to
Carbon::parse('2010-01-01 23:59:59.999999)
no matter the timezone you set for those Carbon
instance.
If you want to know if it's holiday or business day in somewhere else in the world, you have to convert it:
The same goes for opening hours, let's say you want to know if you call center based in Toronto is available from Tokyo at a given hour (Tokyo timezone), you would get something like:
All versions of business-time with dependencies
cmixin/business-day Version ^1.16.1
spatie/opening-hours Version ^2.9 || ^3.0.0 || ^4.0.0