PHP code example of cmixin / business-day

1. Go to this page and download the library: Download cmixin/business-day library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cmixin / business-day example snippets




use Cmixin\BusinessDay;

// You can select one of our official list
$baseList = 'us-national'; // or region such as 'us-il'

// You can add/remove days (optional):
$additionalHolidays = [
    'independence-day' => null, // Even if it's holiday, you can force it to null to make your business open
    'boss-birthday'    => '09-26', // Close the office on September 26th
    // you can also use slash if you prefer day first '26/09' (September 26th too)
    'julian-christmas' => '= julian 12-25', // We support many calendars such as the Julian calendar
    // We support expressions
    'special-easter'   => '= Tuesday before easter',
    'last-monday'      => '= last Monday of October',
    'conditional'      => '= 02-25 if Tuesday then next Friday', // We support conditions
    // And we support closures:
    'very-special'     => function ($year) {
        if ($year === 2020) {
            return '01-15';
        }

        return '02-15';
    },
];

// You can optionally specify some days that are worked even if on weekend
$extraWorkDays = [
    'moving-office'  => '2021-01-17', // year specific
    'yearly-meeting' => '01-25',      // or any year rather it's weekend or not
];

BusinessDay::enable('Carbon\Carbon', $baseList, $additionalHolidays, $extraWorkDays);
// Or if you use Laravel:
// BusinessDay::enable('Illuminate\Support\Carbon', $baseList, $additionalHolidays);

// And you can even enable multiple classes at once:
BusinessDay::enable([
    'Carbon\Carbon',
    'Carbon\CarbonImmutable',
], $baseList, $additionalHolidays);



use Cmixin\BusinessDay;

BusinessDay::enable('Carbon\Carbon');
// Or if you use Laravel:
// BusinessDay::enable('Illuminate\Support\Carbon');

Carbon::parse('2000-12-25 00:00:00')->isHoliday(); // false
Carbon::setHolidaysRegion('us');
Carbon::parse('2000-12-25 00:00:00')->isHoliday(); // true
Carbon::getHolidaysRegion(); // 'us-national' (national is the default region of a country code)

Carbon::setHolidaysRegion('us');

foreach (Carbon::getYearHolidays(2020) as $id => $holiday) {
    echo $holiday->getHolidayName().': '.$holiday->format('l, F j, Y')."\n";
}

// If Carbon::getYearHolidays() is called without argument, current year is used instead.

Carbon::getHolidays('us'); // raw us-national holidays list
Carbon::setHolidaysRegion('fr');
Carbon::getHolidays(); // raw fr-national holidays list

Carbon::setHolidays('us-il', array_merge(
    Carbon::getHolidays('us-national'),
    [
        // Presidents' Day
        'presidents-day' => '= third monday of february', // can be a datetime expression
        // Columbus Day
        'columbus-day' => function ($year) { // can be a closure
            $date = new DateTime("second monday of october $year");

            return $date->format('d/m');
        },
        // Day after Thanksgiving
        'thanksgiving-next-day' => '= fourth thursday of november $year +1 day', // '$year' will be replaced by current year
    ]
));
Carbon::setHolidays('my-enterprise', array_merge(
    Carbon::getHolidays('us-is'),
    [
        // Lincoln's Birthday
        'lincolns-birthday' => '= 02-12 substitute', // substitute will shift the holiday until it does not fall on Saturday, Sunday or an other holiday
        'company-closed-day' => '= 04-05 if friday then previous wednesday', // custom rules can be applied with if/then (if can be followed by a day name or "weekend")
    ]
));
// Then when you select my-enterprise, all us-national,
// us-il and my-enterprise days are enabled
Carbon::setHolidaysRegion('my-enterprise');

Carbon::setHolidays('my-enterprise', [
    'lincolns-birthday' => [
        'date'     => '12/02',
        'observed' => true,
        'name'     => [
            'en' => "Lincoln's Birthday",
            'fr' => 'Anniversaire du Président Lincoln',
        ],
    ],
]);

Carbon::addHolidays('my-list', [
    'poney'        => '20/01',
    'swimmingpool' => '21/01',
]);
Carbon::addHolidays('my-list', [
    'boss-birthday'          => '10/02',
    'boss-birthday-next-day' => '11/02',
]);
Carbon::getHolidays('my-list') // contains 20/01, 21/01, 10/02 and 11/02

Carbon::setHolidaysRegion('us-national');
Carbon::parse('2018-01-15')->isHoliday() // true for 2018
Carbon::parse('2018-01-21')->isHoliday() // false for 2018
Carbon::parse('2019-01-15')->isHoliday() // false for 2019
Carbon::parse('2019-01-21')->isHoliday() // true for 2019
Carbon::isHoliday() // true if today is a holiday

Carbon::setHolidaysRegion('us-national');
Carbon::parse('2018-12-25')->getHolidayId() // "christmas"
Carbon::parse('2018-01-15')->getHolidayId() // "mlk-day"
// Returns false if the day is not a holiday
Carbon::parse('2018-01-21')->getHolidayId() // false

$nextWeekHoliday = Carbon::today()->addWeek()->getHolidayId();
if ($nextWeekHoliday === 'easter' or $nextWeekHoliday === 'christmas') {
  echo 'Time to get chocolates.';
}

Carbon::setHolidaysRegion('fr-national');
Carbon::parse('2018-12-25')->getHolidayName() // "Christmas"
Carbon::parse('2018-12-25')->getHolidayName('fr') // "Noël"

Carbon::setLocale('nl');
Carbon::parse('2018-01-15')->getHolidayName() // "Eerste Kerstdag"

// If the name is not translated in business-day
Carbon::setLocale('de');
Carbon::parse('2018-01-15')->getHolidayName() // "Christmas"

// With Carbon 2, you can use local locale:
Carbon::parse('2018-01-15')->locale('sl')->getHolidayName() // "Božič"

Carbon::parse('2018-12-25')->getHolidayName() // "Christmas"
Carbon::setHolidayName('christmas', 'en', 'Christmas Day');
Carbon::parse('2018-12-25')->getHolidayName() // "Christmas Day"

BusinessDay::enable('Carbon\Carbon', 'us-national', [
  'company-creation' => '05-15',
]);

Carbon::setHolidayName('company-creation', [
  'en' => 'The Day The Company Was Created',
  'fr' => "Le jour de la création de l'Entreprise",
]);

Carbon::setHolidaysRegion('us-national');
Carbon::parse('2018-01-15')->isBusinessDay() // false for 2018 (Martin Luther King Jr. Day, sleep)
Carbon::parse('2018-01-21')->isBusinessDay() // false for 2018 (sunday is week-end by default, sleep)
Carbon::parse('2019-01-15')->isBusinessDay() // true for 2019 (tuesday, nothing special, go to work)
Carbon::parse('2019-01-21')->isBusinessDay() // false for 2019 (Martin Luther King Jr. Day, sleep)
Carbon::isBusinessDay() // true if today is a business day

Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->nextBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-13')->nextBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-15')->nextBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-16')->nextBusinessDay()->format('Y-m-d'); // 2018-01-17
echo Carbon::parse('2018-01-12 12:30:00')->nextBusinessDay()->format('Y-m-d H:i'); // 2018-01-16 12:30 (time is kept)
// If you don't want to keep the time, just use ->startOfDay()
echo Carbon::nextBusinessDay()->format('Y-m-d'); // returns the next business day date after today (midnight)

Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->previousBusinessDay()->format('Y-m-d'); // 2018-01-11
echo Carbon::parse('2018-01-13')->previousBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-15')->previousBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-16')->previousBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-15 12:30:00')->previousBusinessDay()->format('Y-m-d H:i'); // 2018-01-12 12:30 (time is kept)
// If you don't want to keep the time, just use ->startOfDay()
echo Carbon::previousBusinessDay()->format('Y-m-d'); // returns the previous business day date before today (midnight)

Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->currentOrNextBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-13')->currentOrNextBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-15')->currentOrNextBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-16')->currentOrNextBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-13 12:30:00')->currentOrNextBusinessDay()->format('Y-m-d H:i'); // 2018-01-16 12:30 (time is kept)
// If you don't want to keep the time, just use ->startOfDay()
echo Carbon::currentOrNextBusinessDay() // equivalent to Carbon::today()->currentOrNextBusinessDay()

Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->currentOrPreviousBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-13')->currentOrPreviousBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-15')->currentOrPreviousBusinessDay()->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-16')->currentOrPreviousBusinessDay()->format('Y-m-d'); // 2018-01-16
echo Carbon::parse('2018-01-13 12:30:00')->currentOrPreviousBusinessDay()->format('Y-m-d H:i'); // 2018-01-16 12:30 (time is kept)
// If you don't want to keep the time, just use ->startOfDay()
echo Carbon::currentOrPreviousBusinessDay() // equivalent to Carbon::today()->currentOrPreviousBusinessDay()

Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-10')->addBusinessDays(4)->format('Y-m-d'); // 2018-01-17
echo Carbon::parse('2018-01-13')->addBusinessDays()->format('Y-m-d'); // 2018-01-16 add 1 business day by default
echo Carbon::addBusinessDays(6)->format('Y-m-d'); // returns the date 6 business days after today (midnight)

Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-17')->subBusinessDays(4)->format('Y-m-d'); // 2018-01-12
echo Carbon::parse('2018-01-15')->subBusinessDays()->format('Y-m-d'); // 2018-01-12 sub 1 business day by default
echo Carbon::subBusinessDays(5)->format('Y-m-d'); // returns the date 5 business days date before today (midnight)

Carbon::setHolidaysRegion('us-national');

Carbon::setObservedHolidaysZone('Nevada-subsidiary-company');
Carbon::observeAllHolidays();
Carbon::unobserveHoliday('independence-day');

Carbon::setObservedHolidaysZone('Illinois-subsidiary-company');
Carbon::observeAllHolidays();
Carbon::unobserveHolidays(['labor-day', 'mlk-day']);

Carbon::setObservedHolidaysZone('Nevada-subsidiary-company');
Carbon::parse('2018-01-15')->isObservedHoliday(); // true
Carbon::parse('2018-07-04')->isObservedHoliday(); // false
Carbon::parse('2018-09-03')->isObservedHoliday(); // true

Carbon::setObservedHolidaysZone('Illinois-subsidiary-company');
Carbon::parse('2018-01-15')->isObservedHoliday(); // false
Carbon::parse('2018-07-04')->isObservedHoliday(); // true
Carbon::parse('2018-09-03')->isObservedHoliday(); // false

Carbon::getObservedHolidaysZone(); // "Illinois-subsidiary-company"

Carbon::setHolidaysRegion('fr-national');
Carbon::parse('2018-01-15')->isHoliday(); // true
// By default no holiday is observed
Carbon::parse('2018-01-15')->isObservedHoliday(); // false
// Then you can observe a list of holidays
Carbon::unobserveHolidays(['christmas', 'new-year']);
Carbon::parse('2018-01-15')->isObservedHoliday(); // true
// Observed holidays are kept if you change the holidays region:
Carbon::setHolidaysRegion('us-national');
Carbon::parse('2018-01-15')->isObservedHoliday(); // true
// So you can observe holidays that are not in the current region with no restriction
// You can check observance by ID:
Carbon::isObservedHoliday('christmas'); // false
Carbon::isObservedHoliday('labor-day'); // true
Carbon::isObservedHoliday(); // check if today is an observed holiday

Carbon::observeHoliday(['christmas', 'new-year']);
Carbon::observeHoliday('easter');

Carbon::observeHolidays(['christmas', 'new-year']);
Carbon::observeHolidays('easter');

Carbon::unobserveHoliday(['christmas', 'new-year']);
Carbon::unobserveHoliday('easter');

Carbon::unobserveHolidays(['christmas', 'new-year']);
Carbon::unobserveHolidays('easter');

// Observe every holidays except Easter
Carbon::observeAllHolidays();
Carbon::unobserveHoliday('easter');

// Observe only Easter
Carbon::unobserveAllHolidays();
Carbon::observeHoliday('easter');

$openDays = Carbon::diffInBusinessDays('December 25'); // If only one date, [now] is used as the second date

echo "You have to work $openDays days until Christmas.\n";

$days = Carbon::parse('2019-06-10')->diffInBusinessDays(Carbon::parse('2019-06-18')->endOfDay());

echo "If you ask to leave from 2019-06-10 to 2019-06-18, it will cost to you $days days of paid vacation.\n";
// Note the ->endOfDay() to 

echo Carbon::getBusinessDaysInMonth(); // Open days count in the current month
echo Carbon::getBusinessDaysInMonth('2019-06'); // Open days count in June 2019
echo Carbon::parse('2019-06-10')->getBusinessDaysInMonth(); // Can be called from an instance

print_r(Carbon::getMonthBusinessDays()); // All open days in the current month
print_r(Carbon::getMonthBusinessDays('2019-06')); // Open days in June 2019
print_r(Carbon::parse('2019-06-10')->getMonthBusinessDays()); // Can be called from an instance

Carbon::setHolidaysRegion('cn-national');

Carbon::parse('2021-02-07')->isExtraWorkday(); // true

// Global way
Carbon::setBusinessDayChecker(function (CarbonInterface $date) {
    // As an example, you can customize it so
    // every first Wednesday of month is always worked even if it's an holiday:
    if ($date->isWednesday() && $date->copy()->subWeek()->month !== $date->month) {
        return true;
    }

    return $date->isExtraWorkday() || ($date->isWeekday() && !$date->isHoliday());
});

// Single object config (prior to global)
$date = Carbon::parse('2020-12-03');
$date->setBusinessDayChecker($someFunction);

$date->isExtraWorkday() or ($date->isWeekday() and !$date->isHoliday())

// Global way
Carbon::setHolidayGetter(function (string $region, CarbonInterface $self, callable $fallback) {
    [$country, $state] = explode('-', $region);
    $date = $self->format('Y-m-d');

    // Assuming the API you use does not support dates after 2040, you can fallback to the default internal data
    if ($self->year > 2040) {
        return $fallback();
    }

    $holidayData = file_get_contents("https://someholidaysapi.com/holidays/?country=$country&state=$state&date=$date");

    return $holidayData
        ? $holidayData['name'] // The function should return a unique string (like a name or an ID)
        : false; // or false if the day is not a holiday
});

// Single object config (prior to global)
$date = Carbon::parse('2020-12-03');
$date->setHolidayGetter($someFunction);

Carbon::setHolidayDataById('christmas', [
    'info' => 'It may be cold in USA',
]);

Carbon::parse('2020-12-25')->setHolidayData([
    'info' => 'It may be cold in USA',
]);

Carbon::getHolidayDataById('christmas')

Carbon::parse('2020-12-25')->getHolidayData()

Carbon::getHolidaysAvailableRegions()

 return [
  'holidays' => [
    'region' => 'us',
    'with' => [
      'boss-birthday' => '09-26',
      'last-monday'   => '= last Monday of October',
    ],
    // extra work days (even if in the weekend)
    'without' => [
      'exceptional-work-day' => '2021-01-03',
    ],
  ],
];

Carbon::parse('2010-01-01 02:30', 'Europe/Paris')->setTimezone('America/Toronto')->isHoliday() // false
Carbon::parse('2010-01-01 12:30', 'Europe/Paris')->setTimezone('America/Toronto')->isHoliday() // true