#native_company# #native_desc#
#native_cta#

Finding Holidays

By Alan Meadows
on June 1, 2001

Version: .99

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: Here’s a couple of functions that will allow you to define some holidays and their dates. You can also define dates for non-fixed holidays (eg. 3rd sunday of June).

function GetTimeStamp($MySqlDate)
  {
  /*
  Take a date in yyyy-mm-dd format and return it to the user in a PHP timestamp
  Robin 06/10/1999
  */
	
  $date_array = explode("-",$MySqlDate); // split the array
	
  $var_year = $date_array[0];
  $var_month = $date_array[1];
  $var_day = $date_array[2];

  $var_timestamp = mktime(0,0,0,$var_month,$var_day,$var_year);
  return($var_timestamp); // return it to the user
  }  // End function GetTimeStamp()

function ordinalDay($ord, $day, $month, $year)
  // ordinalDay returns date of the $ord $day of $month.
  // For example ordinalDay(3, 'Sun', 5, 2001) returns the
  // date of the 3rd Sunday of May (ie. Mother's Day).
  // 
  // Note: $day must be the 3 char abbr. for the day, as
  //       given by date("D"); 
  //
  // By: [email protected]

  {
  $firstOfMonth = GetTimeStamp("$year-$month-01");
  $lastOfMonth  = $firstOfMonth + date("t", $firstOfMonth) * 86400;
  $dayOccurs = 0;
	
  for ($i = $firstOfMonth; $i < $lastOfMonth ; $i += 86400)
     {
     if (date("D", $i) == $day)
       {
       $dayOccurs++;
       if ($dayOccurs == $ord)
         { $ordDay = $i; } 
       }
     }
  return $ordDay;
  }  // End function ordinalDay()

function getNextHoliday()
   // Looks through a lists of defined holidays and tells you which 
   // one is coming up next.
   //
   // By: [email protected]
   {
   $year = date("Y");

   class holiday
     {
     var $name;
     var $date;
     var $catNum;
			
     function holiday($name, $date, $catNum)
        // Contructor to define the details of each holiday as it is created.
        {
        $this->name   = $name;   // Official name of holiday
        $this->date   = $date;   // UNIX timestamp of date
        $this->catNum = $catNum; // category, we used for databases access
        }
     } // end class holiday
			
   $holidays[] = new holiday("Groundhog Day", GetTimeStamp("$year-2-2"), "20");
   $holidays[] = new holiday("Valentine's Day", GetTimeStamp("$year-2-14"), "14");
   $holidays[] = new holiday("St. Patrick's Day", GetTimeStamp("$year-3-17"), "15");
   $holidays[] = new holiday("Easter", easter_date($year), "16");
   $holidays[] = new holiday("Mother's Day", ordinalDay(2, 'Sun', 5, $year), "3");
   $holidays[] = new holiday("Father's Day", ordinalDay(3, 'Sun', 6, $year), "4");
   $holidays[] = new holiday("Independence Day", GetTimeStamp("$year-7-4"), "17");
   $holidays[] = new holiday("Christmas", GetTimeStamp("$year-12-25"), "13");

   $numHolidays = count($holidays);
   for ($i = 0; $i < $numHolidays; $i++) 
     { 
     if ( date("z") > date("z", $holidays[$i]->date) && date("z") <= date("z",
          $holidays[$i+1]->date) ) 
        { 
        $nextHoliday["name"]      = $holidays[$i+1]->name; 
        $nextHoliday["dateStamp"] = $holidays[$i+1]->date;
        $nextHoliday["dateText"]  = date("F j, Y", $nextHoliday["dateStamp"]);
        $nextHoliday["num"]       = $holidays[$i+1]->catNum;		 
        }
     }
   return $nextHoliday;
   } // end function GetNextHoliday


$nextHoliday = getNextHoliday();
echo $nextHoliday["name"]." (".$nextHoliday["dateText"].")";