Zend - The PHP Company




Date & Time

Add Code


Number of Work Days between Dates  

Type: code fragment
Added by: gmrcomp
Entered: 29/07/2004
Last modified: 08/12/2003
Rating: - (fewer than 3 votes)
Views: 7537
This function returns the number of work days between two dates.


<?
function date_difference($start_date$end_date$workdays_only false$skip_holidays false){
    
$start_date strtotime($start_date);
    
$end_date strtotime($end_date);
    
$seconds_in_a_day 86400;
    
$sunday_val "0";
    
$saturday_val "6";
    
$workday_counter 0;
    
$holiday_array = array();
    
    
$ptr_year intval(date("Y"$start_date));
    
$holiday_array[$ptr_year] = getHolidays(date("Y"$start_date));

    for(
$day_val $start_date$day_val <= $end_date$day_val+=$seconds_in_a_day){
        
$pointer_day date("w"$day_val);
        if(
$workdays_only == true){
            if((
$pointer_day != $sunday_val) AND ($pointer_day != $saturday_val)){
                if(
$skip_holidays == true){
                    if(
intval(date("Y"$day_val))!=$ptr_year){
                        
$ptr_year intval(date("Y"$day_val));
                        
$holiday_array[$ptr_year] = getHolidays(date("Y"$day_val));
                    }
                    if(!
in_array($day_val$holiday_array[date("Y"$day_val)])){
                        
$workday_counter++;    
                    }
                }else{
                    
$workday_counter++;    
                }
            }
        }else{
                if(
$skip_holidays == true){
                    if(
intval(date("Y"$day_val))!=$ptr_year){
                        
$ptr_year intval(date("Y"$day_val));
                        
$holiday_array[$ptr_year] = getHolidays(date("Y"$day_val));
                    }
                    if(!
in_array($day_val$holiday_array[date("Y"$day_val)])){
                        
$workday_counter++;    
                    }
                }else{
                    
$workday_counter++;    
                }
        }
    }
    return 
$workday_counter;
}

/*--------------------------------------------------------------
The following code is a modified version of heymeadows' zend script submitted on 11/06/2001

CHANGE LOG
2004-07-28 - removed date category numbers
2004-07-28 - renamed function GetNextHoliday to getHolidays
2004-07-28 - moved holiday class outside function
2004-07-28 - modified structure & return value of getHolidays function
2004-07-28 - added memorial_day function to return the timestamp of memorial day of the given year
    
--------------------------------------------------------------*/
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 memorial_day($inc_year){
    for(
$date_stepper intval(date("t"strtotime("$inc_year-05-01"))); $date_stepper >= 1$date_stepper--){
        if(
date("l"strtotime("$inc_year-05-$date_stepper"))=="Monday"){
            return 
strtotime("$inc_year-05-$date_stepper");
            break;    
        }    
    }
}


// Looks through a lists of defined holidays and tells you which
// one is coming up next.
//
// By: heymeadows@yahoo.com
function getHolidays($inc_year){
    
//$year = date("Y");
    
$year $inc_year;
    
    
$holidays[] = new holiday("New Year's Day"GetTimeStamp("$year-1-1"));
    
$holidays[] = new holiday("Birthday of Martin Luther King"ordinalDay(3'Mon'1$year));
    
$holidays[] = new holiday("Groundhog Day"GetTimeStamp("$year-2-2"));
    
$holidays[] = new holiday("Valentine's Day"GetTimeStamp("$year-2-14"));
    
$holidays[] = new holiday("St. Patrick's Day"GetTimeStamp("$year-3-17"));
    
$holidays[] = new holiday("Easter"easter_date($year));
    
$holidays[] = new holiday("Memorial Day"memorial_day($year));
    
$holidays[] = new holiday("Mother's Day"ordinalDay(2'Sun'5$year));
    
$holidays[] = new holiday("Father's Day"ordinalDay(3'Sun'6$year));
    
$holidays[] = new holiday("Independence Day"GetTimeStamp("$year-7-4"));
    
$holidays[] = new holiday("Labor Day"ordinalDay(1'Mon'9$year));
    
$holidays[] = new holiday("Thanksgiving Day"ordinalDay(4'Thu'11$year));
    
$holidays[] = new holiday("Christmas"GetTimeStamp("$year-12-25"));
    
    
$numHolidays count($holidays) - 1;
    
$out_array = array();
    
    for (
$i 0$i $numHolidays$i++){
            
$out_array[] = $holidays[$i]->date;
    }
    unset(
$holidays);
    return 
$out_array;
// end function getHolidays

class holiday{
    var 
$name;
    var 
$date;

    
// Contructor to define the details of each holiday as it is created.
    
function holiday($name$date){
        
$this->name   $name;   // Official name of holiday
        
$this->date   $date;   // UNIX timestamp of date
    
}
// end class holiday
?>


Usage Example


// date difference
echo date_difference("06/05/99", date("m/d/Y"), false, true);

echo "n";

// date difference (workdays only)
echo date_difference("01/01/2004", date("m/d/Y"), true, false);


Rate This Script





Search



This Category All Categories