1 回答
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
棘手的部分是星期五,您没有真正提到,但我为其添加了测试用例。
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报