i have record set pulling various pieces of information sql database including dates. i'd work out number of working days specific date todays date. i'm working out how many working days have passed since first date.
i don't have issue working out working days, there many helpful posts on here. issue have need recur on each line in table filled database. variables different each row, page blank after first result, because assume script trying run again using same named variables.
is there way enclose php code sum can run in next row same variables without previous row interfering?
thanks in advance help.
<?php //the function returns no. of business days between 2 dates , skips holidays function getworkingdays($startdate,$enddate){ // strtotime calculations once $enddate = strtotime($enddate); $startdate = strtotime($startdate); //the total number of days between 2 dates. compute no. of seconds , divide 60*60*24 //we add 1 inlude both dates in interval. $days = ($enddate - $startdate) / 86400 + 1; $no_full_weeks = floor($days / 7); $no_remaining_days = fmod($days, 7); //it return 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 leap years when february has 29 days, equal sign added here //in first case whole interval within week, in second 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 fix edge 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 leap years gave remainder of 0 still calculated weekends between first , last day, 1 way fix $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--; } return $workingdays; } $approval = date('y-m-d',strtotime($row_rsworksorders1['intraartapproval'])); $w_days = getworkingdays($approval,(date('y-m-d'))) . ' days'; echo $w_days; ?>
the date being pulled database $row_rsworksorders1['intraartapproval'])
Comments
Post a Comment