在codeigniter的考勤管理系统中,我想将2个迟到标记计算为1半天,3个迟到标记计算为1全天缺勤。该怎么做呢?
$latemark_halfday= $this->Crud_model->GetData('attendence','late_time',"emp_id='".$_SESSION['SESSION_NAME']['id']."' and LEFT(date,7)='".date('Y-m')."'",'','','','');
$checklate= count($latemark_halfday)/2;
发布于 2019-09-09 17:46:52
我想这是最干净的方式:
/**
* @param array $latemark_halfday
* An array of late marks.
* @return array
* The first item is the calculated fullday absent.
* The second item is the calculated halfday absent.
*/
function calculate_absent($latemark_halfday) {
$fullday_absent = (int) floor(count($latemark_halfday) / 3);
$halfday_absent = (int) floor((count($latemark_halfday) % 3) / 2);
return [$fullday_absent, $halfday_absent];
}
您可以使用单元测试来检查此函数:
<?php
$tests = [
['input' => [1], 'expected' => [0, 0]],
['input' => [1, 1], 'expected' => [0, 1]],
['input' => [1, 1, 1], 'expected' => [1, 0]],
['input' => [1, 1, 1, 1], 'expected' => [1, 0]],
['input' => [1, 1, 1, 1, 1], 'expected' => [1, 1]],
['input' => [1, 1, 1, 1, 1, 1], 'expected' => [2, 0]],
['input' => [1, 1, 1, 1, 1, 1, 1], 'expected' => [2, 0]],
['input' => [1, 1, 1, 1, 1, 1, 1, 1], 'expected' => [2, 1]],
];
foreach ($tests as $test) {
$latemark_halfday = $test['input'];
$count = count($latemark_halfday);
list($expected_fullday, $expected_halfday) = $test['expected'];
list($fullday_absent, $halfday_absent) = calculate_absent($latemark_halfday);
// tests
if ($expected_fullday !== $fullday_absent) throw new Exception("count: $count: fullday: expected $expected_fullday, got $fullday_absent"));
if ($expected_halfday !== $halfday_absent) throw new Exception("count: $count: halfday: expected $expected_halfday, got $halfday_absent"));
echo "latemarks: $count, fullday: $fullday_absent, halfday: $halfday_absent [pass]\n";
}
结果:
latemarks: 1, fullday: 0, halfday: 0 [pass]
latemarks: 2, fullday: 0, halfday: 1 [pass]
latemarks: 3, fullday: 1, halfday: 0 [pass]
latemarks: 4, fullday: 1, halfday: 0 [pass]
latemarks: 5, fullday: 1, halfday: 1 [pass]
latemarks: 6, fullday: 2, halfday: 0 [pass]
latemarks: 7, fullday: 2, halfday: 0 [pass]
latemarks: 8, fullday: 2, halfday: 1 [pass]
https://stackoverflow.com/questions/57849204
复制相似问题