Create an array of holidays for any given year!


#1

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]


#2

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…


#3

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;
}