spatie/holidays
Calculate public holidays for supported countries in PHP. Fetch holidays for the current or a specific year using an ISO country code or country class (with region-based holidays). Simple API: Holidays::for(...)->get() returns an array of holiday dates/names.
Installation
composer require spatie/holidays
Add to config/app.php under providers (if not using auto-discovery):
Spatie\Holidays\HolidaysServiceProvider::class,
First Use Case Fetch holidays for a country (e.g., Belgium) in the current year:
use Spatie\Holidays\Holidays;
$holidays = Holidays::for('be')->get();
// Returns an array of Carbon instances for holidays.
Key Classes to Know
Holidays: Main facade for fetching holidays.Holiday: Represents a single holiday (extends Carbon).HolidayCollection: Collection of holidays (extends Illuminate\Support\Collection).Where to Look First
$holidays = Holidays::for('us')->get(); // All US holidays for current year
$holidays = Holidays::for('de', now()->year + 1)->get();
$holidays = Holidays::for('us', 'ca')->get(); // California holidays
if (Holidays::for('fr')->isHoliday($date)) {
// Handle holiday logic
}
$holidays->where('name', 'like', 'Christmas');
$holidays->where('type', 'national'); // 'national', 'regional', 'observed'
$this->app->bind(HolidayChecker::class, function ($app) {
return new HolidayChecker('be');
});
public function handle($request, Closure $next) {
if (Holidays::for('be')->isHoliday(now())) {
return redirect()->route('holiday-page');
}
return $next($request);
}
Holidays::for('be')->addHoliday(
now()->copy()->setDate(2023, 12, 26),
'Boxing Day (Custom)'
);
Holidays::for('be')->overrideHolidays([/* custom array */]);
trans() or Carbon’s setLocale():
$holiday->setLocale('fr')->translatedFormat('l, F jS');
Cache holidays for performance (e.g., in a boot() method):
public function boot() {
Cache::remember('holidays_be', now()->year, function () {
return Holidays::for('be')->get();
});
}
Store user’s country in a model and fetch holidays dynamically:
$user->country; // e.g., 'jp'
$holidays = Holidays::for($user->country)->get();
if (Holidays::for('us')->isHoliday(now())) {
Feature::disable('shipping');
}
if ($holiday->isObserved()) {
// Handle observed holiday logic
}
Use Holidays::fake() in tests to mock holidays:
Holidays::fake([
'be' => [
now()->copy()->setDate(2023, 1, 1),
],
]);
$this->assertTrue(Holidays::for('be')->isHoliday(now()));
us, de, jp). Avoid nicknames like usa or germany.us (e.g., us-ca) and ca (e.g., ca-on). Other regions may throw exceptions.Holidays::for('be', 2020)->get(); // Past year
$holiday->isObserved().Carbon instances in the UTC timezone by default. Convert to a local timezone if needed:
$holiday->setTimezone('Europe/Brussels');
$holidays->where('type', 'national');
us) include state-specific holidays (e.g., us-ca for California).addHoliday() are not persisted across requests. Re-add them if needed (e.g., in a service provider’s boot() method).Holidays::get() (deprecated). Use Holidays::for('be')->get() instead.HolidayNotFoundException if the country isn’t supported.Holidays::for('be', 2023)->get()).be for Belgium, not belgium).Carbon::setTimezone() to debug:
$holiday->timezone; // Check current timezone
$holiday->setTimezone('America/New_York')->format('Y-m-d');
use Spatie\Holidays\HolidayProvider;
class CustomHolidayProvider extends HolidayProvider {
protected function configure(): void {
$this->name('Custom Holiday');
$this->addHoliday(now()->copy()->setDate(2023, 12, 25), 'Custom Day');
}
}
config/holidays.php:
'providers' => [
\Spatie\Holidays\HolidayProviders\Belgium::class,
App\Providers\CustomHolidayProvider::class,
],
config/holidays.php:
'overrides' => [
'be' => [
'name' => [
'2023-12-25' => 'Kerstmis (Custom)',
],
],
],
type (e.g., national, regional, observed). Use this to filter:
$nationalHolidays = Holidays::for('jp')->get()->where('type', 'national');
How can I help you explore Laravel packages today?