Author Topic: Create an array of holidays for any given year!  (Read 1820 times)

Strider64

  • Professional PHP Helper
  • Senior Member
  • *
  • Posts: 946
  • Karma: 77
  • Don't Use mysql....use mysqli or PDO!
    • View Profile
    • Pepster's Place
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  <?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 
'<pre>' print_r($holidays1) . '</pre>';
 
 

Insanity: doing the same thing over and over again and expecting different results -> https://www.pepster.com

PHP Help Forum

Create an array of holidays for any given year!
« on: April 27, 2015, 02:29:10 pm »



PHP Help Forum

Re: Create an array of holidays for any given year!
« Reply #1 on: June 26, 2015, 12:49:20 pm »



PHP Help Forum

Re: Create an array of holidays for any given year!
« Reply #2 on: December 11, 2015, 07:50:30 am »