mysql - Calculate time difference in seconds excluding weekends and a time period in PHP -
i have given on trying in mysql , instead i'm going inject result array of results (which used later in js app).
i have loop go through each result:
$data = $result->fetchall(); foreach($data $i => $row) { // $data[$i]['age'] = $row['age']; }
i want add seconds between $data[$i]['age']
datetime , current datetime.
normally easy how exclude weekends , time between 16:30 , 07:30?
i don't think specific or uninteresting, here answer 1 of colleagues:
public static function calculatetime($startdate, $enddate) {//returns seconds passed date_default_timezone_set('europe/london'); //opening hours - can pull database depending on users working hours $workinghoursopen = new datetime('07:30:00'); $workinghoursclose = new datetime('16:30:00'); //time worked , $timestarted = strtotime(date('h:i:s', strtotime($enddate))); $timefinished = strtotime(date('h:i:s', strtotime($startdate))); $workingseconds = $workinghoursclose->gettimestamp() - $workinghoursopen->gettimestamp(); $workingsecondsv2 = $timefinished - $timestarted; //option send array of holidays (3rd param) $workingdays = util::getworkingdays(date('y-m-d', strtotime($startdate)), date('y-m-d', strtotime($enddate)), array()); $totalworkingseconds = $workingdays * $workingseconds; //working days * 9 hours $secondsclosed = 0; $i = 0; while ($i < $workingdays) { $secondsclosed = $secondsclosed - (15 * 3600); $i++; } $secondspassed = ($workingsecondsv2 - $totalworkingseconds) + (9 * 3600); $secondspassed = -1 * ($secondspassed); // invert number (was giving -xx) return $secondspassed; } public static function getworkingdays($startdate, $enddate, $holidays) { // strtotime calculations once $enddate = strtotime($enddate); $startdate = strtotime($startdate); $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; }
works 100% , can expanded bring in work hours database.
Comments
Post a Comment