Thursday, 4 June 2015

PHP: Functions used to define a schedule of holidays. Can define non-fixed holidays

(eg. 3rd sunday of June).

<?php
function GetTimeStamp($MySqlDate)
  {

    
  $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");
  //

  {
  $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.
   //
   {
   $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"].")";

?>

0 comments:

Post a Comment