Create an array of holidays for any given year!

I wrote a small Class that gives the major holidays in an array using OOP obviously, for I am developing an online calendar and daily appointment booking. So I thought I share this little script to save people some time, who are developing their own calendars and what have you:

[php]<?php

class Holidays {
public $holidays = [
‘new_years_day’ => NULL,
‘easter_sunday’ => NULL,
‘memorial_day’ => NULL,
‘fourth_of_july’ => NULL,
‘labor_day’ => NULL,
‘halloween’ => NULL,
‘thanksgiving_day’ => NULL,
‘christmas_day’ => NULL
];

protected $year = NULL;
    

public function __construct($year = 2015) {
    $this->holidays = $this->generate_holidays($year);
}

protected function generate_holidays($year) {
    
    $this->year = new DateTime('1-1-' . $year,  new DateTimeZone('America/Detroit')); // Set the year that is to generate the holidays:
    
    /* New Year's Day */
    $this->holidays['new_years_day'] = $this->year->format('l, F j, Y');
    /* Easter Sunday */       
    $this->holidays['easter_sunday'] = $this->year->modify('@' . easter_date($this->year->format("Y")))->format('l, F j, Y');
   /* Memorial Day */
    $this->holidays['memorial_day'] = $this->year->modify('Last monday of may')->format('l, F j, Y');
   /* Fourth of July */
   $this->holidays['fourth_of_july'] = $this->year->modify('July 4, ' . $year)->format('l, F j, Y');
   /* Labor Day */
   $this->holidays['labor_day'] = $this->year->modify('First monday of september')->format('l, F j, Y');
   /* Halloween */
   $this->holidays['halloween'] = $this->year->modify('October 31, ' . $year)->format('l, F j, Y');
   /* Thanksgiving Day */
   $this->holidays['thanksgiving_day'] = $this->year->modify('Fourth thursday of November')->format('l, F j, Y');
   /* Christmas Day */
   $this->holidays['christmas_day'] = $this->year->modify('December 25, ' . $year)->format('l, F j, Y');
   /* Return Array to Constructor */
    return $this->holidays;
}

}

/* Create a new instance of the class Holidays with the year you want for the holidays */
$myDate = new Holidays(2016);

/* Grab the array of dates of the holidays for that given year */
$holidays = $myDate->holidays;

echo ‘

’ . print_r($holidays, 1) . ‘
’;

[/php]

Strider64, wasn’t there a public class somewhere where you can pull the holidays from for the US?
Seems like I remember that. I did a lot with calendars a couple years ago and seems like there was
a way to pull them in from somewhere.

Also, I remember using fullcalendar-1.5.3 which was a nice add-on library with a lot of simple code to
allow you to mark days for events and recurring items like birthdays.

You always come up with nice classes. keep them coming… Thanks, Ernie…

function buildHolidayArray($year){

 $holidays = array();
 // new years day
 array_push($holidays, mktime(1,1,1, 1, 1, $year));
 // christmas day
array_push($holidays, mktime(1,1,1, 12, 25, $year));
// easter
array_push($holidays, easter_date($year));

return $holidays;
}

Updated the original code to just Federal holidays. Also added check to see if the date falls on a Sunday then to declare Monday as the Holiday. There is also another rule that if the holiday falls on a Saturday then the Friday is the holiday. For what I was working on, that was not a consideration so did not include that check, but easy to add.

<?php
class Holidays {
    public $holidays = [
    ];
    protected $year = NULL;

    public function __construct($year = 2015) {
        $this->holidays = $this->generate_holidays($year);
    }

    protected function generate_holidays($year) {
        $this->year = new DateTime('1-1-' . $year,  new DateTimeZone('America/Detroit')); // Set the year that is to generate the holidays:

        /* New Year's Day */
        $dow = $this->year->format('w');
        if ($dow != '0')
            $this->holidays['new_years_day'] = $this->year->format('l, F j, Y');
        else
            $this->holidays['new_years_day'] = $this->year->modify('January 2, ' . $year)->format('l, F j, Y');
        /* MLK Day */
        $this->holidays['mlk_day'] = $this->year->modify('Third Monday of January')->format('l, F j, Y');
        /* Washington's Birthday */
        $this->holidays['wash_day'] = $this->year->modify('Third Monday of February')->format('l, F j, Y');
        /* Easter Sunday */
        $this->holidays['memorial_day'] = $this->year->modify('Last monday of may')->format('l, F j, Y');
        /* Fourth of July */
        $dow = $this->year->modify('July 4, ' . $year)->format('w');
        if ($dow != '0')
            $this->holidays['fourth_of_july'] = $this->year->modify('July 4, ' . $year)->format('l, F j, Y');
        else
            $this->holidays['fourth_of_july'] = $this->year->modify('July 5, ' . $year)->format('l, F j, Y');
        /* Labor Day */
        $this->holidays['labor_day'] = $this->year->modify('First monday of september')->format('l, F j, Y');
        /* Columbus Day */
        $this->holidays['columbus_day'] = $this->year->modify('Second Monday of October')->format('l, F j, Y');
        /* Veterans Day */
        $dow = $this->year->modify('November 11, ' . $year)->format('w');
        if ($dow != '0')
            $this->holidays['veterans_day'] = $this->year->modify('November 11, ' . $year)->format('l, F j, Y');
        else
            $this->holidays['veterans_day'] = $this->year->modify('November 12, ' . $year)->format('l, F j, Y');
        /* Thanksgiving Day */
        $this->holidays['thanksgiving_day'] = $this->year->modify('Fourth thursday of November')->format('l, F j, Y');
        /* Christmas Day */
        $dow = $this->year->modify('December 25, ' . $year)->format('w');
        if ($dow != '0')
            $this->holidays['christmas_day'] = $this->year->modify('December 25, ' . $year)->format('l, F j, Y');
        else
            $this->holidays['christmas_day'] = $this->year->modify('December 26, ' . $year)->format('l, F j, Y');
       /* Return Array to Constructor */
        return $this->holidays;
    }
}

for ($year = 2020; $year < 2030; $year++) {
    /* Create a new instance of the class Holidays with the year you want for the holidays */
    $myDate = new Holidays($year);

    /* Grab the array of dates of the holidays for that given year */
    $holidays = $myDate->holidays;

    echo "\n", '---------- ', $year, ' ------------' , "\n";
    echo print_r($holidays, 1), "\n";
}
Sponsor our Newsletter | Privacy Policy | Terms of Service