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]