1 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
我將發(fā)布我的函數(shù)版本。您傳遞票證提交的日期時(shí)間并獲取允許響應(yīng)的日期時(shí)間。
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;
}
我用來(lái)在不同條件下測(cè)試這個(gè)函數(shù)的代碼:
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
棘手的部分是星期五,您沒(méi)有真正提到,但我為其添加了測(cè)試用例。
- 1 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)