亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據工作時間和工作日的分鐘數計算 SLA

根據工作時間和工作日的分鐘數計算 SLA

PHP
慕勒3428872 2023-08-11 15:31:03
我正在嘗試構建一個函數,該函數根據工作時間計算票務服務協議,并跳過非工作時間和周末的計算,并將時間相應地轉移到第二天或下一個工作日。我對工單的響應 SLA 是 30 分鐘,如果工單插入日期在工作日 {周一至周五} 和工作時間內 {08:00:00 - 17:00:00},那么我的結束日期應根據以下公式計算因素:對于周一至周四,如果 insertDate 落在工作時間內,則時間應移至工作時間的下 30 分鐘,例如 if case1: insertDate = '2020-07-16 16:00:00'; 在這里,我得到了預期的結果,endDate ='2020-07-16 16:30:00'; 如果 insertDate 時間戳超出工作時間窗口,例如 if case2: insertDate = '2020-07-16 16:50:00'; 在這里,我沒有得到預期的結果,應該是 endDate ='2020-07-17 08:20:00';對于周末(周六至周日),在該周末窗口創建的任何工單,應從周一 08:00:00 開始計算 case3: insertDate = '2020-07-18 16:50:00'; 在這里,我沒有得到預期的結果,應該是 endDate ='2020-07-20 08:30:00';下面是我的代碼,適用于 case1 很好,ubt 則適用于 case2 和 case3,對此的任何幫助都非常感謝。<?phpfunction calculateSLA($totalMinutes){$insertDate = '2020-07-16 16:00:00';$endDate = date('Y-m-d H:00:00',strtotime($insertDate)); //$t_min = date('i',strtotime($insertDate));$BusinessStart = '08:00:00';$BusinessEnd   = '17:00:00';$i           = 1;$flag        = false;while ($totalMinutes > 0) {    $day = date('D', strtotime($endDate)); // fetching day of week    if ($day == 'Sat') { // checking if saturday thenskip by adding 2 day to end date        $endDate = date('Y-m-d', strtotime($endDate . " +2 Day")) . ' ' . $BusinessStart; // adding 2 day if saturday        continue;    }    $diff  = strtotime($BusinessEnd) - strtotime(date("H:i:s", strtotime($insertDate))); // getting difference of time of office end date and result end date    var_dump($diff);    $mins = $diff / (60); // difference in mins.    if ($mins > $totalMinutes) {        $mins = $totalMinutes;        $flag  = true;     } else {        $mins = $totalMinutes - $mins; // substracting mins from total minutes left            }    $endDate = date('Y-m-d H:i:s', strtotime("+$mins Minute", strtotime($insertDate))); // adding subtracted minutes    if (!$flag) {        $endDate = date('Y-m-d', strtotime($insertDate . " +1 Day")) . ' ' . $BusinessStart; // if not last loop add day to result end date    } else {        break;    }}echo $endDate;}calculateSLA(30);?>
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

我將發布我的函數版本。您傳遞票證提交的日期時間并獲取允許響應的日期時間。


function calculateSLA(DateTime $reportDate): DateTime {

  $responseDate = (clone $reportDate);


  // check conditions and add 1 minute to provided date 30 times (so 30 minutes)

  for($i=0; $i<30;$i++) {

    // if time is before 8:00 (working hours) skip to 8:00

    if ($responseDate->format('G') < 8) {

      $responseDate->setTime(8, 0);

    }


    // if time is after 17:00 (working hours) skip to next day at 8:00

    if ($responseDate->format('G') >= 17) {

      $responseDate->add(new DateInterval('PT15H'));

      $responseDate->setTime(8, 0);

    }


    // if at any time it is weekend skip to monday at 8:00

    if (in_array($responseDate->format('D'), ['Sat', 'Sun'])) {

      $responseDate = $responseDate->modify('next monday 8:00');

    }


    $responseDate->add(new DateInterval('PT1M'));

  }


  return $responseDate;

}

我用來在不同條件下測試這個函數的代碼:


function test(string $date, string $expected) {

  $result = calculateSLA(new DateTime($date));

  echo 'date: '.$date.', expected: '.$expected.', got: '.$result->format('Y-m-d H:i:s').' '.($result->format('Y-m-d H:i:s') === $expected ? 'OK' : 'ERRROR').PHP_EOL;

}

test('2020-07-16 16:00:00', '2020-07-16 16:30:00'); // weekday during hours

test('2020-07-16 16:50:00', '2020-07-17 08:20:00'); // weekday during hours until next day

test('2020-07-18 16:50:00', '2020-07-20 08:30:00'); // weekend

test('2020-07-16 06:50:00', '2020-07-16 08:30:00'); // weekday before working hours

test('2020-07-16 20:50:00', '2020-07-17 08:30:00'); // weekday after working hours

test('2020-07-17 16:50:00', '2020-07-20 08:20:00'); // friday during working hours until monday

test('2020-07-17 17:50:00', '2020-07-20 08:30:00'); // friday after hours

輸出:


date: 2020-07-16 16:00:00, expected: 2020-07-16 16:30:00, got: 2020-07-16 16:30:00 OK

date: 2020-07-16 16:50:00, expected: 2020-07-17 08:20:00, got: 2020-07-17 08:20:00 OK

date: 2020-07-18 16:50:00, expected: 2020-07-20 08:30:00, got: 2020-07-20 08:30:00 OK

date: 2020-07-16 06:50:00, expected: 2020-07-16 08:30:00, got: 2020-07-16 08:30:00 OK

date: 2020-07-16 20:50:00, expected: 2020-07-17 08:30:00, got: 2020-07-17 08:30:00 OK

date: 2020-07-17 16:50:00, expected: 2020-07-20 08:20:00, got: 2020-07-20 08:20:00 OK

date: 2020-07-17 17:50:00, expected: 2020-07-20 08:30:00, got: 2020-07-20 08:30:00 OK

棘手的部分是星期五,您沒有真正提到,但我為其添加了測試用例。


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號