计算轮班时数

时间:2011-05-26 10:07:36

标签: php

我正在尝试根据工作小时数和工作时间来计算某人的工作时间。

例如:

班次模式是07:00到15:00是'早上',15:00到23:00是'下午',23:00到07:00是'晚上'。

因此,如果我在08:00开始并在17:30结束,这意味着我将在早上7点和下午2点开始付款。

5 个答案:

答案 0 :(得分:2)

我从一段时间以前开发的项目中修改了一段代码。

函数'intersection'计算两个范围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";

答案 1 :(得分:1)

你可以有3个计数器,每个班次一个。那么你需要一种方法来增加小时和每小时增量。你检查它是否在每个班次中,如果它在某个班次之内,那么你增加那个计数器。

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

答案 2 :(得分:1)

这就是我为了好玩而一起扔的。

还有很大的改进空间,可以轻松扩展以提供更多的计算,您的想象力是极限。

由于'night1'和'night2'对我来说没有任何意义,因此我可以选择移除它们的命名键,但我选择将它们删除: - )

<?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";

?>

答案 3 :(得分:0)

这是另一种方式:(我假设unix时间戳的开始和结束时间)

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

答案 4 :(得分:0)

我的轮班时间比较简单:22:00 - &gt; 07:00 =夜间工作,07:00 - &gt; 22:00 =日常工作

但如果轮班时间如22:30那么我对Javi R版本的回答错误 - &gt; 08:00(结果:天= 0小时,夜晚= 8:30小时)和20:00 - &gt; 10:00(结果:天= 2小时,夜晚= 9小时),但正确的答案是班次时间,如23:00 - &gt; 07:00(结果:天= 0小时,夜晚= 7小时)。

对此答复感到抱歉。我没有足够的代表,但我需要让它发挥作用。

相关问题