A simple and elegant PHP library for working with commonly used or desired dates in your applications. UsefulDates helps you quickly identify and retrieve important dates like holidays, birthdays, paydays, and other significant calendar events.
This is an evolution of geoffreyrose/us-holidays.
- Flexible Date Definitions - Add your own dates with simple or complex logic
- Extensions Support - Create reusable date collections for country-specific holidays, birthdays, or any custom dates
- Business Day Helpers - Built-in methods for working with business days
- Filtering - Filter dates by custom properties with various operators
- Laravel Integration - Works seamlessly with Laravel via auto-discovered Facade
On its own, UsefulDates comes with no predefined dates. It provides a clean base that you can extend with your own dates and methods.
- PHP 8.4+
- Carbon
composer require geoffreyrose/useful-datesuse UsefulDates\UsefulDates;
use Carbon\Carbon;
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::now());
// Add a simple date
$usefulDates->addDate(name: "Patrick Star's Birthday", date: Carbon::createFromFormat('Y-m-d', '1999-08-17'));
// Get upcoming dates
$dates = $usefulDates->getUsefulDatesInDays(30);Laravel uses Package Auto-Discovery, so no manual registration is needed:
use UsefulDates\Facades\UsefulDates;
$usefulDates = UsefulDates::setDate(Carbon::now());You can create custom date definitions by extending UsefulDateAbstract. Each custom date class must implement the date() method which returns the date for the current context year.
Important: The
date()method should always return dates in UTC time.
Create a new class that extends UsefulDates\Abstracts\UsefulDateAbstract:
<?php
namespace App\Dates;
use Carbon\Carbon;
use UsefulDates\Abstracts\UsefulDateAbstract;
use UsefulDates\Enums\RepeatFrequency;
class AprilFools extends UsefulDateAbstract
{
public function __construct()
{
$this->name = "April Fools' Day";
$this->start_date = Carbon::createFromFormat('Y-m-d', '1582-04-01');
$this->is_repeated = true;
$this->repeat_frequency = RepeatFrequency::YEARLY;
}
public function date(): Carbon
{
return Carbon::createFromFormat('Y-m-d', "{$this->currentDate->year}-04-01");
}
}Tip:
Carbon::createFromFormat()is faster thanCarbon::create()in my benchmarks.
Use the add() method to register your custom date class:
use UsefulDates\UsefulDates;
use Carbon\Carbon;
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::now());
$usefulDates->add(\App\Dates\AprilFools::class);
$nextDates = $usefulDates->getNextUsefulDates(5);
// Returns the next 5 occurrences of April Fools' DaySet the working date/time context (normalized to UTC). Accepts Carbon, DateTime, or a string parsable by Carbon.
use UsefulDates\UsefulDates;
use Carbon\Carbon;
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::now());
// Strings and DateTime also work
$usefulDates->setDate('2025-02-01');
$usefulDates->setDate(new \DateTime('2025-03-01 08:00'));Register a UsefulDate by class name. The class must extend UsefulDateAbstract.
$usefulDates->add(\App\Dates\AprilFools::class);Add a simple UsefulDate without creating a class. Creates an internal definition that repeats according to the provided options.
Parameters:
$name- Human-friendly name for the date$date- Prototype date (month/day are used for each occurrence)$isRepeated- Whether the date repeats (default:true)$repeatFrequency-RepeatFrequency::YEARLY,MONTHLY,NONE, orCUSTOM(default:YEARLY)$startYear- First year the date is considered (default: year from$date)
$usefulDates->addDate(
name: "Patrick Star's Birthday",
date: Carbon::createFromFormat('Y-m-d', '1999-08-17'),
);Register an extension (must extend UsefulDatesExtensionAbstract). Adds its useful dates and any custom methods exposed by the extension.
$usefulDates->addExtension(\UsefulDatesUsHolidays\UsefulDatesUsHolidaysExtension::class);Returns true if any registered UsefulDate matches the given date (defaults to the current context date).
$usefulDates->setDate(Carbon::now());
if ($usefulDates->isUsefulDate()) {
// today matches a useful date
}
$someDate = Carbon::create(2025, 12, 25);
$isUseful = $usefulDates->isUsefulDate($someDate);Returns a list of matching UsefulDate objects for the given date (defaults to the current context date).
$dates = $usefulDates->getUsefulDate(Carbon::create(2025, 1, 1));
foreach ($dates as $date) {
echo $date->name . ' occurs on ' . $date->usefulDate();
}See the Filtering section for filter options.
Get all matching UsefulDates occurring from $startDate (default: current context) through $startDate + $days.
// Next 30 days from current context
$usefulDates->setDate(Carbon::now());
$next30 = $usefulDates->getUsefulDatesInDays(30);
// From a specific start date
$start = Carbon::create(2025, 1, 1);
$dates = $usefulDates->getUsefulDatesInDays(60, $start);Get all useful dates within the next N years from the current context date.
$usefulDates->setDate(Carbon::now());
$twoYears = $usefulDates->getUsefulDatesInYears(2);Get the next N useful dates after the current context date.
$usefulDates->setDate(Carbon::now());
$next5 = $usefulDates->getNextUsefulDates(5);Get the previous N useful dates before the current context date.
$prev3 = $usefulDates->getPreviousUsefulDates(3);Get all useful dates within a given calendar year (defaults to current context year).
$byYear = $usefulDates->getUsefulDatesByYear(2025);Default business days are Monday–Friday ([1, 2, 3, 4, 5]). Days use Carbon's constants where 0 = Sunday through 6 = Saturday.
Returns true if the configured business days are Monday–Friday.
$usefulDates->setBusinessDays([1, 2, 3, 4, 5]);
$usefulDates->isStandardBusinessDays(); // trueDefine which days of week are business days (0=Sun..6=Sat). Throws InvalidDayException for invalid values.
$usefulDates->setBusinessDays([1, 2, 3, 4, 6]); // Mon-Thu + SatGet the configured business days array.
$days = $usefulDates->getBusinessDays(); // [1, 2, 3, 4, 5]Check if the given date (or current context date) is a business day.
$usefulDates->setDate(Carbon::create(2025, 5, 10)); // Saturday
$usefulDates->setBusinessDays([1, 2, 3, 4, 5]);
$usefulDates->isBusinessDay(); // falseGet the next business day after the current context date.
$usefulDates->setDate(Carbon::create(2025, 5, 10)); // Saturday
$usefulDates->nextBusinessDay()->toDateString(); // "2025-05-12"Get the previous business day before the current context date.
$usefulDates->setDate(Carbon::create(2025, 5, 10)); // Saturday
$usefulDates->prevBusinessDay()->toDateString(); // "2025-05-09"Returns today if it is a business day, otherwise the previous business day.
$usefulDates->todayOrPreviousBusinessDay()->toDateString();Returns today if it is a business day, otherwise the next business day.
$usefulDates->todayOrNextBusinessDay()->toDateString();When creating custom date classes by extending UsefulDateAbstract, you have access to these properties:
| Property | Type | Default | Description |
|---|---|---|---|
$name |
string |
(required) | Human-friendly name for the date |
$additional_search_names |
array |
[] |
Alternative names for searching |
$is_repeated |
bool |
false |
Whether the date repeats |
$repeat_frequency |
RepeatFrequency |
NONE |
YEARLY, MONTHLY, NONE, or CUSTOM |
$start_date |
?Carbon |
null |
First date the event is considered |
$end_date |
?Carbon |
null |
Last date the event is considered |
You may also add any additonal properties to your custom dates as needed
These methods are available on each UsefulDate instance:
Returns the date if it should be considered for the current context (applies repeat frequency and start/end rules), otherwise null.
Returns the number of days from the current context to this date. Positive = future, negative = past, 0 = today.
use UsefulDates\UsefulDates;
use Carbon\Carbon;
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::create(2025, 1, 1));
$usefulDates->addDate("Patrick Star's Birthday", Carbon::createFromFormat('Y-m-d', '1999-08-17'));
$usefulDates->addDate('April Fools Day', Carbon::createFromFormat('Y-m-d', '1582-04-01'));
$dates = $usefulDates->getUsefulDatesInYears(10);
foreach ($dates as $date) {
echo $date->name . ' - ' . $date->usefulDate()->toDateString() . ' (' . $date->daysAway() . ' days away)';
}Extensions allow you to bundle groups of UsefulDates together, optionally with custom methods.
| Extension | Description |
|---|---|
| geoffreyrose/useful-dates-us-holidays | US Holidays |
use UsefulDates\UsefulDates;
use Carbon\Carbon;
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::now());
$usefulDates->addExtension(\UsefulDatesUsHolidays\UsefulDatesUsHolidaysExtension::class);Create a class that extends UsefulDatesExtensionAbstract:
<?php
namespace App\Extensions;
use App\Dates\AprilFools;
use UsefulDates\Abstracts\UsefulDatesExtensionAbstract;
class MyHolidays extends UsefulDatesExtensionAbstract
{
public static string $name = 'My Holidays';
public static string $description = 'Custom holiday collection';
public static bool $hasMethods = true;
// Return array of UsefulDate class names
public static function usefulDates(): array
{
return [
AprilFools::class,
];
}
// Optional: Add custom methods
public function methods(): array
{
return [
'isDecember' => [$this, 'isDecember'],
];
}
public function isDecember(): bool
{
return $this->usefulDates->date->month === 12;
}
}Using the extension with custom methods:
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::now()->addMonths(2));
$usefulDates->addExtension(\App\Extensions\MyHolidays::class);
if ($usefulDates->isDecember()) {
echo "It's December!";
}For better IDE autocompletion with extension methods, create a wrapper class:
Note: The @mixin annotation is what helps IDEs understand the extension methods.
<?php
namespace App;
use UsefulDates\UsefulDates;
use UsefulDatesUsHolidays\UsefulDatesUsHolidaysExtension;
use Carbon\Carbon;
/**
* @mixin UsefulDatesUsHolidaysExtension
*/
class MyUsefulDates extends UsefulDates
{
public function init(Carbon $date): self
{
$this->setDate($date);
$this->addExtension(UsefulDatesUsHolidaysExtension::class);
return $this;
}
}$dates = new \App\MyUsefulDates();
$dates->init(Carbon::now());Filter useful dates by custom properties using the $filters parameter available on most retrieval methods.
Each filter is an array with three keys:
| Key | Description |
|---|---|
property |
The property name on the UsefulDate class |
operator |
Comparison operator: >, <, >=, <=, =, != |
value |
The value to compare against |
use UsefulDates\UsefulDates;
use Carbon\Carbon;
$usefulDates = new UsefulDates();
$usefulDates->setDate(Carbon::now());
$usefulDates->addExtension(\UsefulDatesUsHolidays\UsHolidaysExtension::class);
// Get federal holidays established before 1900
$federalHolidays = $usefulDates->getUsefulDatesInDays(100, filters: [
[
'property' => 'is_federal_holiday',
'operator' => '=',
'value' => true,
],
[
'property' => 'federal_holiday_start_year',
'operator' => '<=',
'value' => 1900,
],
]);Multiple filters are combined with AND logic (all conditions must match).
./vendor/bin/pint# Run tests
./vendor/bin/pest
# Run tests with coverage
./vendor/bin/pest --coverage-html coverage
# With Laravel Herd
herd coverage ./vendor/bin/pest --coverage-html coverageMIT License. See LICENSE for details.