Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算轮班工作时间_Php - Fatal编程技术网

Php 计算轮班工作时间

Php 计算轮班工作时间,php,Php,我试图根据工作小时数和他们工作的时间段来计算某人的工作时间 例如: 轮班模式为07:00至15:00为“上午”,15:00至23:00为“下午”,23:00至07:00为“夜间” 因此,如果我在08:00开始工作,在17:30结束工作,这意味着我将获得7个“上午”小时和2.5个“下午”小时的工资。您可以有3个计数器,每班一个。然后你需要一种按小时递增的方法,每递增一小时。检查它是否在每个班次内,如果在某个班次内,则递增该计数器 最后,每个计数器的值应该是您在每个班次中的工作量。还有另一种方法:(

我试图根据工作小时数和他们工作的时间段来计算某人的工作时间

例如:

轮班模式为07:00至15:00为“上午”,15:00至23:00为“下午”,23:00至07:00为“夜间”


因此,如果我在08:00开始工作,在17:30结束工作,这意味着我将获得7个“上午”小时和2.5个“下午”小时的工资。

您可以有3个计数器,每班一个。然后你需要一种按小时递增的方法,每递增一小时。检查它是否在每个班次内,如果在某个班次内,则递增该计数器

最后,每个计数器的值应该是您在每个班次中的工作量。

还有另一种方法:(我假设unix时间戳中的开始和结束时间)

  • 获取员工的开始和结束日期
  • 通过一个简单的公式得到工作时间:$ending_hour-$starting_hour
    • 如果工作时间超过8小时,员工应获得额外报酬
    • 计算下一班次的额外小时数:$ending_hour-$next_shifts_starting_hour

我改编了我不久前开发的一个项目中的一段代码

函数“交叉点”计算两个范围s1-e1和s2-e2之间重叠的时间量

请注意,所有时间都以秒为单位,当时间在第二天时,我们添加3600*24秒

<?php
function intersection($s1, $e1, $s2, $e2)
{
        if ($e1 < $s2)
                return 0;
        if ($s1 > $e2)
                return 0;
        if ($s1 < $s2)
                $s1 = $s2;
        if ($e1 > $e2)
                $e1 = $e2;
        return $e1 - $s1;
}

        $start = strtotime("07:00");
        $end = strtotime("17:30");
        // $end = strtotime("05:30") + 3600*24; // the work ended at 05:30 morning of the next day

        $morning_start = strtotime("07:00");
        $morning_end = strtotime("15:00");

        $afternoon_start = strtotime("15:00");
        $afternoon_end = strtotime("23:00");

        $night_start = strtotime("23:00");
        $night_end = strtotime("07:00") + 3600*24; // 07:00 of next day, add 3600*24 seconds

        echo "morning: " . intersection( $start, $end, $morning_start, $morning_end ) / 3600 . " hours\n";
        echo "afternoon: " . intersection( $start, $end, $afternoon_start, $afternoon_end ) / 3600 . " hours\n";
        echo "night: " . intersection( $start, $end, $night_start, $night_end ) / 3600 . " hours\n";

这就是我刚才为了好玩而拼凑的东西

有很大的改进空间,可以很容易地扩展到提供更多的计算,你的想象力是有限的

为了可读性,可以为移位数组指定命名键,但我选择删除它们,因为“night1”和“night2”对我来说毫无意义:-)


我的轮班时间更简单:22:00->07:00=夜班,07:00->22:00=日班

但如果轮班时间为22:30->08:00(结果:白天=0小时,晚上=8:30小时)和20:00->10:00(结果:白天=2小时,晚上=9小时),我在Javi R版本中得到了错误的答案,但轮班时间为23:00->07:00(结果:白天=0小时,晚上=7小时)的答案是正确的


很抱歉回答这个问题。我没有足够的rep,但我需要让它工作。

源数据的格式是什么?这是数据库表行还是其他什么?我投票赞成这一点,因为这是一些很好的建议,但Demon的代码给了我一些开始的东西,希望我能听从我的意愿。非常感谢。
<?php

$shift_data = array(
    array(
        'rate' => 12.5,
        'start' => strtotime('00:00:00'),
        'end' => strtotime('07:00:00'),
    ),
    array(
        'rate' => 7.55,
        'start' => strtotime('07:00:00'),
        'end' => strtotime('15:00:00'),
    ),
    array(
        'rate' => 10,
        'start' => strtotime('15:00:00'),
        'end' => strtotime('23:00:00'),
    ),
    array(
        'rate' => 12.5,
        'start' => strtotime('23:00:00'),
        'end' => strtotime('07:00:00') + 86400, // next morning
    ),
);

function calculateWage($start, $end, $rate) {
    $result = array();

    $result['time']['seconds'] = $end - $start;
    $result['time']['minutes'] = $result['time']['seconds'] / 60;
    $result['time']['hours'] = $result['time']['minutes'] / 60;
    $result['wages'] = $result['time']['hours']  * $rate;

    //print_r($result);
    return $result;
}

$shift_start = strtotime('08:00');
$shift_end = strtotime('17:30');
$shift_wages = 0;

foreach ($shift_data as $shift) {
    if ($shift['start'] <= $shift_end) {
        $start = ($shift_start <= $shift['start']) ? $shift['start'] : $shift_start;
        $end = ($shift_end <= $shift['end']) ? $shift_end : $shift['end'];
        $shift_wage = calculateWage($start, $end, $shift['rate']);
        $shift_wages = $shift_wages + $shift_wage['wages'];
    }
}

echo "\nTotal wages for today: $" . number_format($shift_wages, 2, '.', ',') . "\n\n";

?>