我需要计算给定开始日期的结束日期/服务水平协议(SLA),并考虑完成时间,以分钟为单位,在工作时间/日期之内。
例如:
考虑每天8个工作小时(上午9点至下午5点)和星期日的周末。结束日期应为2019-04-01 11:00:00。
所以总小时数是12,
在此方面的任何帮助将不胜感激。
到目前为止,我能够获得除周末以外的总工作日,但获得确切的结束时间是我苦苦挣扎的地方。
谢谢。
更新:
$bookingDateTime = Carbon::parse('2019-03-29 15:00:00');
$i = 0;
$completion_in_days = (720/60)/8; //converted minutes in no of days
$working_days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
while($i < ($completion_in_days)){
if(array_search(strtolower($bookingDateTime->englishDayOfWeek), $working_days) !== false){
$i++;
}
$bookingDateTime->addDay();
}
这将给我结束日期。
答案 0 :(得分:1)
请在下面找到该代码段,我逐行写注释以供理解
$endDate = $startDate = '2019-03-29 15:00:00';
$officeStart = '09:00:00';
$officeEnd = '17:00:00';
$totalHours = 12;
$i = 1;
$flag = false;
while ($totalHours > 0) {
$day = date('D', strtotime($endDate)); // fetching day of week
if ($day == 'Sun') { // checking if sunday thenskip by adding 1 day to end date
$endDate = date('Y-m-d', strtotime($endDate . " +1 Day")) . ' ' . $officeStart; // adding one day if sunday
continue;
}
$diff = strtotime($officeEnd) - strtotime(date("H:i:s", strtotime($endDate))); // getting difference of time of office end date and result end date
$hours = $diff / (3600); // difference in minutes
if ($hours > $totalHours) {
$hours = $totalHours;
$flag = true; // to break loop if last loop comes
} else {
$totalHours = $totalHours - $hours; // substracting hours from total hours left
}
$endDate = date('Y-m-d H:i:s', strtotime("+$hours Hour", strtotime($endDate))); // adding hours which are substracted
if (!$flag) {
$endDate = date('Y-m-d', strtotime($endDate . " +1 Day")) . ' ' . $officeStart; // if not last loop add day to result end date
} else {
break;
}
}
输出
2019-04-01 11:00:00
Demo。