note 72952 added to function.date
| From: | marius at svr dot ro | Date: | Mon, 05 Feb 2007 18:54:27 +0000 |
| Subject: | note 72952 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122890@lists.php.net to get a copy of this message | ||
Re: steve at somejunkwelike dot com
I think this is a better function to find the business days between two dates. This function
requires PHP 5.1.0.
<?php
//The function returns the no. of business days between two dates and it skeeps the holidays
function getWorkingDays($startDate,$endDate,$holidays){
//The total number of days between the two dates. We compute the no. of seconds and divide it to
60*60*24
//We add one to inlude both dates in the interval.
$days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
$no_full_weeks = floor($days / 7);
$no_remaining_days = fmod($days, 7);
//It will return 1 if it's Monday,.. ,7 for Sunday
$the_first_day_of_week = date("N",strtotime($startDate));
$the_last_day_of_week = date("N",strtotime($endDate));
//The two can't be equal because the $no_remaining_days (the interval between
$the_first_day_of_week and $the_last_day_of_week) is at most 6
//In the first case the whole interval is within a week, in the second case the interval falls in
two weeks.
if ($the_first_day_of_week < $the_last_day_of_week){
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
}
else{
if ($the_first_day_of_week <= 6) $no_remaining_days--;
//In the case when the interval falls in two weeks, there will be a Sunday for sure
$no_remaining_days--;
}
//The no. of business days is: (number of weeks between the two dates) * (5 working days) + the
remainder
$workingDays = $no_full_weeks * 5 + $no_remaining_days;
//We subtract the holidays
foreach($holidays as $holiday){
$time_stamp=strtotime($holiday);
//If the holiday doesn't fall in weekend
if (strtotime($startDate) <= $time_stamp && $time_stamp <= strtotime($endDate)
&& date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
$workingDays--;
}
return $workingDays;
}
//Example:
$holidays=array("2006-12-25","2006-12-26","2007-01-01");
echo getWorkingDays("2006-12-22","2007-01-06",$holidays)
// => will return 8
?>
----
Server IP: 69.147.83.197
Probable Submitter: 82.76.67.70
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/72952
Del: integrated -- https://master.php.net/note/delete/72952/integrated
Del: useless -- https://master.php.net/note/delete/72952/useless
Del: bad code -- https://master.php.net/note/delete/72952/bad+code
Del: spam -- https://master.php.net/note/delete/72952/spam
Del: non-english -- https://master.php.net/note/delete/72952/non-english
Del: in docs -- https://master.php.net/note/delete/72952/in+docs
Del: other reasons-- https://master.php.net/note/delete/72952
Reject -- https://master.php.net/note/reject/72952
Search -- https://master.php.net/manage/user-notes.php