php - Calculate business days -
i need method adding "business days" in php. example, fri 12/5 + 3 business days = quarta-feira 12/10.
at minimum need code understand weekends, ideally should business relationship federal holidays well. i'm sure come solution brute forcefulness if necessary, i'm hoping there's more elegant approach out there. anyone?
thanks.
here's function user comments on date() function page in php manual. it's improvement of before function in comments adds back upwards jump years.
enter starting , ending dates, along array of holidays might in between, , returns working days integer:
<?php //the function returns no. of business days between 2 dates , skips holidays function getworkingdays($startdate,$enddate,$holidays){ // strtotime calculations 1 time $enddate = strtotime($enddate); $startdate = strtotime($startdate); //the total number of days between 2 dates. compute no. of seconds , split 60*60*24 //we add together 1 inlude both dates in interval. $days = ($enddate - $startdate) / 86400 + 1; $no_full_weeks = floor($days / 7); $no_remaining_days = fmod($days, 7); //it homecoming 1 if it's monday,.. ,7 sunday $the_first_day_of_week = date("n", $startdate); $the_last_day_of_week = date("n", $enddate); //---->the 2 can equal in jump years when feb has 29 days, equal sign added here //in first case whole interval within week, in sec case interval falls in 2 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 { // (edit tokes prepare border case start day sunday // , end day not saturday) // day of week start later day of week end if ($the_first_day_of_week == 7) { // if start date sunday, subtract 1 day $no_remaining_days--; if ($the_last_day_of_week == 6) { // if end date saturday, subtract day $no_remaining_days--; } } else { // start date saturday (or earlier), , end date (mon..fri) // skip entire weekend , subtract 2 days $no_remaining_days -= 2; } } //the no. of business days is: (number of weeks between 2 dates) * (5 working days) + remainder //---->february in none jump years gave remainder of 0 still calculated weekends between first , lastly day, 1 way prepare $workingdays = $no_full_weeks * 5; if ($no_remaining_days > 0 ) { $workingdays += $no_remaining_days; } //we subtract holidays foreach($holidays $holiday){ $time_stamp=strtotime($holiday); //if holiday doesn't fall in weekend if ($startdate <= $time_stamp && $time_stamp <= $enddate && date("n",$time_stamp) != 6 && date("n",$time_stamp) != 7) $workingdays--; } homecoming $workingdays; } //example: $holidays=array("2008-12-25","2008-12-26","2009-01-01"); echo getworkingdays("2008-12-22","2009-01-02",$holidays) // => homecoming 7 ?>
php calendar date
No comments:
Post a Comment