PHPBuilder - Calculate difference in dates, using only workdays



RSS Twitter
Snippets Calendars Dates

Calculate difference in dates, using only workdays

by: Jake Cantrell
|
April 25, 2003

Version: 1.0

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: This Function will take two dates, and calculate the difference between them in terms of working days, excluding holidays. I'm sure that someone could take this and clean it up a bit to make it run faster. I just needed something asap. Based on cronky's Working Days Function. I added modifications from heymeadows Finding Holidays function, and some other little stuff.



<?

//Based on cronky's Working Days Function. 
//I added modifications from heymeadows Finding Holidays function, and some other little stuff.  
//Jake Cantrell : Senior Programmer MyGov.us
//jcantrell@mygov.us

//This script was designed to calculate the business days that a permit has been open
//since the application process.

//We will be hiring more programmers soon, email me if you think you would be a good //candidate to come work on the MyGov team.
//Our team is located in Ada, OK.
//Our website is http://www.mygov.us (Project Launch Date is August 1, 2003)



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; } 
       }
     }
	 
	 $ordDay = (($ordDay - $firstOfMonth) / 86400) + 1;
if (strlen($ordDay) < 2) { $ordDay = "0$ordDay"; }
return $ordDay;
  }  // End function ordinalDay()

//DO NOT CHANGE ANYTHING IN THIS SECTION
//Not the most beautiful code... but it should be pretty straight foward
//Send comments, questions or suggestions to nugod@nugod.net

function subDate($begdate, $enddate)
{
if ($enddate == $begdate){return "0";}
else {

//get begdate
$begyear = substr($begdate, 0, 4);
$begmonth = substr($begdate, 5, 2);
$begday = substr($begdate, 8, 2);



//get enddate (I don't think this is relevant, but it is here :) )
$endyear = substr($enddate, 0, 4);
$endmonth = substr($enddate, 5, 2);
$endday = substr($enddate, 8, 2);

//day of begdate
$getday=mktime(0,0,0,$begmonth,$begday,$begyear);
$daynum=date(d, $getday);

//while loop to add workdays and holidays
while ($past != $enddate)
{
$month="$begmonth";
if (strlen($month) < 2) { $month = "0$month"; }
$year=$begyear;
$daynum=$daynum+01;
if (strlen($daynum) < 2) { $daynum = "0$daynum"; }

$past="$year-$month-$daynum";
$pasttime=mktime(0,0,0,$month,$daynum,$year);
$pastdayofweek=date(l,$pasttime);
if ($pastdayofweek=="Monday"){$workday=$workday+1;}
if ($pastdayofweek=="Tuesday"){$workday=$workday+1;}
if ($pastdayofweek=="Wednesday"){$workday=$workday+1;}
if ($pastdayofweek=="Thursday"){$workday=$workday+1;}
if ($pastdayofweek=="Friday"){$workday=$workday+1;}
//END OF FIRST UNEDITABLE SECTION


//THIS SECTION CONTAINS THE HOLIDAYS AND IS WHAT WILL NEED EDITING ONCE A YEAR IF
//YOUR HOLIDAYS CHANGE
$newyear="$year-01-01";
$memday=ordinalDay(4, 'Mon', 5, $year);
$memorialday="$year-05-$memday";
$independenceday="$year-07-04";
$labday=ordinalDay(1, 'Mon', 9, $year);
$laborday="$year-09-$labday";
$tks1=ordinalDay(3, 'Thu', 11, $year);
$thanksgiving="$year-11-$tks1";
$tks2=$tks1 + 1;
$thanksgiving2="$year-11-$tks2";
$xmas1="$year-12-24";
$xmas2="$year-12-25";
//THIS IS THE END OF THE HOLIDAYS SECTION


//If you add extra holidays you will need to put it in here as well
if ($past==$newyear){$holiday=$holiday+1;}
if ($past==$memorialday){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
if ($past==$independenceday){$holiday=$holiday+1;}
if ($past==$laborday){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
if ($past==$thanksgiving){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
if ($past==$thanksgiving2){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;;}}}
if ($past==$xmas1){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
if ($past==$xmas2){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}

if ($begmonth == "02")
	{
	if($daynum == "28")
	{
	$begmonth = $begmonth + 01;
	$daynum = "00";
	}
	}

if ($begmonth == "04")
	{
	if($daynum == "30")
	{
	$begmonth = $begmonth + 01;
	$daynum = "00";
	}
	}
if ($begmonth == "06")
	{
	if($daynum == "30")
	{
	$begmonth = $begmonth + 01;
	$daynum = "00";
	}
	}
if ($begmonth == "09")
	{
	if($daynum == "30")
	{
	$begmonth = $begmonth + 01;
	$daynum = "00";
	}
	}
if ($begmonth == "11")
	{
	if($daynum == "30")
	{
	$begmonth = $begmonth + 01;
	$daynum = "00";
	}
	}

if ($daynum == "31")
	{
	$daynum = 00;
	$begmonth = $begmonth + 01;
	if ($begmonth =="13")
	{
	$begmonth = "01";
	$begyear = $begyear + 1;
	}
	}

}
//prints the elapsed workdays
$totalwork = $workday - $holiday;
return $totalwork;
//Thats it... hope this is useful
}
}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Jake Cantrell

Comment:



Comment:

(Maximum characters: 1200). You have characters left.