首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将2个迟到分数计算为缺勤半天

如何将2个迟到分数计算为缺勤半天
EN

Stack Overflow用户
提问于 2019-09-09 15:01:36
回答 1查看 151关注 0票数 0

在codeigniter的考勤管理系统中,我想将2个迟到标记计算为1半天,3个迟到标记计算为1全天缺勤。该怎么做呢?

代码语言:javascript
运行
复制
$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;
EN

回答 1

Stack Overflow用户

发布于 2019-09-09 17:46:52

我想这是最干净的方式:

代码语言:javascript
运行
复制
/**
 * @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];
}

您可以使用单元测试来检查此函数:

代码语言:javascript
运行
复制
<?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";
}

结果:

代码语言:javascript
运行
复制
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]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57849204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档