PHP Get US Holidays for Multiple Years
Feb 14, 2023ProgrammingComments (0)
Here is a simple PHP script that generates US holidays for multiple years in YYYY-MM-DD format, and stores them in a basic array. Note that due to timezone differences, this may create unexpected results within 24 hours of a new year for users that are in different timezones.

// Settings
$startYear = date('Y'); // Current year
$endYear = date('Y') + 1; // Next year

// Stores the generated holidays
$holidays = [];

// Loop through each year and generate holidays
for ($y = $startYear; $y <= $endYear; $y ++) {
if (!isset($holidays[$y])) {
$holidays[$y] = [];
}
$holidays[$y]["New Year's Day"] = $y . '-01-01';
$holidays[$y]["MLK Day"] = date('Y-m-d', strtotime('Third Monday of January ' . $y));
$holidays[$y]["President's Day"] = date('Y-m-d', strtotime('Third Monday of February ' . $y));
$holidays[$y]["Easter"] = date('Y-m-d', easter_date($y));
$holidays[$y]["Memorial Day"] = date('Y-m-d', strtotime('Last Monday of May ' . $y));
$holidays[$y]["Independence Day"] = $y . '-07-04';
$holidays[$y]["Labor Day"] = date('Y-m-d', strtotime('First Monday of September ' . $y));
$holidays[$y]["Columbus Day"] = date('Y-m-d', strtotime('Second Monday of October ' . $y));
$holidays[$y]["Veterans Day"] = $y . '-11-11';
$holidays[$y]["Thanksgiving"] = date('Y-m-d', strtotime('Fourth Thursday of November ' . $y));
$holidays[$y]["Christmas Day"] = $y . '-12-25';
$holidays[$y]["Christmas Eve"] = $y . '-12-24';
$holidays[$y]["New Year's Eve"] = $y . '-12-31';
}

If you just want to get a single US holiday for the current year, you can use any of these simpler lines:

$newYearsDay = date('Y') . '-01-01';
$mlkDay = date('Y-m-d', strtotime('Third Monday of January ' . date('Y')));
$presidentsDay = date('Y-m-d', strtotime('Third Monday of February ' . date('Y')));
$easter = date('Y-m-d', easter_date());
$memorialDay = date('Y-m-d', strtotime('Last Monday of May ' . date('Y')));
$independenceDay = date('Y') . '-07-04';
$laborDay = date('Y-m-d', strtotime('First Monday of September ' . date('Y')));
$columbusDay = date('Y-m-d', strtotime('Second Monday of October ' . date('Y')));
$veteransDay = date('Y') . '-12-11';
$thanksgiving = date('Y-m-d', strtotime('Fourth Thursday of November ' . date('Y')));
$christmasEve = date('Y') . '-12-24';
$christmasDay = date('Y') . '-12-25';
$newYearsEve = date('Y') . '-12-31';
Comments (0)
Add a Comment
No comments yet