spatie/holidays
Calculate public holidays by country (ISO alpha-2) with a simple API. Get an array of Holiday objects including localized name, CarbonImmutable date, and type (e.g., national). Supports multiple countries and is easy to extend by adding new country definitions.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require spatie/holidays
Requires PHP 8.4+.
First Use Case: Fetch all holidays for Belgium in 2024:
use Spatie\Holidays\Holidays;
$holidays = Holidays::for('be', year: 2024)->get();
foreach ($holidays as $holiday) {
echo $holiday->name . ' (' . $holiday->date->format('Y-m-d') . ')';
}
Outputs:
Nieuwjaar (2024-01-01)
Dodenherdenking (2024-05-27)
...
Key Classes:
Holidays: Facade for country-specific holiday queries.Holiday: Represents a holiday with name, date, type, and region.Belgium, Germany, Ethiopia) for region-specific logic.Holidays::for() methods (get(), getInRange(), isHoliday(), etc.).HolidayType enum (National, Regional, Religious, etc.).$holidays = Holidays::for('us', year: 2024)->get();
$holidays = Holidays::for('ca', year: 2024)->getInRange('2024-07-01', '2024-08-31');
$upcoming = Holidays::for('jp')->getUpcoming(5); // Next 5 holidays
// Ethiopia (now with locale support)
$holidays = Holidays::for('et', year: 2024, locale: 'am')->get(); // Amharic names
use Spatie\Holidays\Countries\Ethiopia;
$ethiopia = Ethiopia::make();
$holidays = Holidays::for($ethiopia, year: 2024, locale: 'en')->get(); // English names
Holidays::for('us')->isHoliday('2024-07-04'); // true (Independence Day)
Holidays::for('us')->isHoliday('2024-07-06'); // true (Observed, falls on Saturday)
$longWeekends = Holidays::for('au', year: 2024)->getLongWeekends(4); // Min 4 days
foreach ($longWeekends as $weekend) {
echo $weekend->startDate->format('Y-m-d') . ' to ' .
$weekend->endDate->format('Y-m-d');
}
// Ethiopia with Amharic locale
$holidays = Holidays::for('et', locale: 'am')->get();
foreach ($holidays as $holiday) {
echo $holiday->name; // e.g., "የኢትዮጵያ አዲስ አዘያም" (Ethiopian New Year)
}
Service Provider Binding:
Bind the Holidays facade in AppServiceProvider for global access:
public function register()
{
$this->app->bind('holidays', function () {
return new \Spatie\Holidays\Holidays();
});
}
Model Observers:
Add holiday checks to models (e.g., Event):
class EventObserver
{
public function saving(Event $event)
{
if ($event->is_workday && Holidays::for('et')->isHoliday($event->date)) {
throw new \Exception('Cannot schedule events on Ethiopian holidays!');
}
}
}
API Responses: Format holidays for JSON APIs (now with locale support):
return response()->json([
'holidays' => Holidays::for('et', year: 2024, locale: 'am')->get()->map(fn ($h) => [
'name' => $h->name,
'date' => $h->date->format('Y-m-d'),
'type' => $h->type->value,
'locale' => 'am', // Explicitly include locale
]),
]);
Caching: Cache holiday data for performance (e.g., 1 year):
$holidays = Cache::remember("holidays.{$country}.{$year}.{$locale}", now()->addYear(), function () use ($country, $year, $locale) {
return Holidays::for($country, year: $year, locale: $locale)->get();
});
| Use Case | Implementation |
|---|---|
| Payroll System | Exclude holidays from workday counts. |
| Event Planning | Block dates on holidays (now with Ethiopian support). |
| Leave Management | Validate leave requests against holidays. |
| Shipping Deadlines | Adjust cutoffs for holiday weekends. |
| Localization | Display holiday names in user's language (e.g., Amharic). |
Region-Specific Quirks:
// ❌ Missing region (may return incomplete data)
Holidays::for('de')->get();
// ✅ Correct
Holidays::for('de', region: 'DE-BW')->get();
Observed Holidays:
// US observes Memorial Day (May 27, 2024) on Monday if it falls on weekend
Holidays::for('us')->isHoliday('2024-05-27'); // true (if weekend)
Year Range Limits:
Holidays::for('sa', year: 2050)->get(); // [] (if 2050 is unsupported)
Locale Fallbacks:
Holidays::for('et', locale: 'es')->get(); // Spanish names (if available)
Holidays::for('et', locale: 'am')->get(); // Amharic names (new in 2.1.0)
Carbon Dependencies:
CarbonImmutable. Ensure your project is compatible:
composer require nesbot/carbon
Validate Country Codes:
if (!Holidays::has('xx')) {
throw new \InvalidArgumentException("Unsupported country: xx");
}
Inspect Holiday Data:
dd(Holidays::for('et', year: 2024, locale: 'am')->get()->first()->toArray());
Check for Regional Overrides:
use Spatie\Holidays\Countries\Ethiopia;
dd(Ethiopia::regions()); // [] (Ethiopia has no regional holidays)
Country class for unsupported countries:
class MyCountry extends \Spatie\Holidays\Countries\
How can I help you explore Laravel packages today?