Zend - The PHP Company




Date & Time

Add Code


Finding upcoming holidays  

Type: code fragment
Added by: heymeadows
Entered: 01/06/2001
Last modified: 06/12/2000
Rating: - (fewer than 3 votes)
Views: 6887
These functions allow you to define a calendar of holidays and find which one comes up next. Holidays can be defined as fixed, or floating (eg. 3d sunday of june.)


<?php
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: heymeadows@yahoo.com

  
{
  
$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: heymeadows@yahoo.com
   
{
   
$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"].")";


?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories