我正在尝试根据工作小时数和工作时间来计算某个人的小时数。计算班次工作时间
例如:
换档模式是07:00至15:00是 '早上',15:00至23:00是 '下午',和23:00至07:00是“夜”。
因此,如果我在08:00开始并在17:30结束,这意味着我可以在早上7点和下午2.5点之间领取薪水。
我正在尝试根据工作小时数和工作时间来计算某个人的小时数。计算班次工作时间
例如:
换档模式是07:00至15:00是 '早上',15:00至23:00是 '下午',和23:00至07:00是“夜”。
因此,如果我在08:00开始并在17:30结束,这意味着我可以在早上7点和下午2.5点之间领取薪水。
我改编自一个项目,我开发了前一段时间的一段代码。
函数'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";
你可以有3个计数器,每个班次一个。然后,您需要一种按小时递增的方式,并且每增加一小时递增一次。你检查它是否在每个班次内,如果它在一个特定的班次内,那么你增加该计数器。
最后,每个计数器的值应该是您在每个班次中的工作量。
已经投票赞成,因为这是一些很好的建议,但恶魔的代码给了我一些开始,并有希望屈服于我的意志。谢谢。 – Rooneyl 2011-05-26 12:00:06
这里是另一种方式:(我假设在Unix时间戳的开始和结束时间)
这就是我为了娱乐而扔在一起。
有很大的改进空间,它可以很容易地扩展到提供更多的计算,你的想象力是极限。
转变的阵列可以给出可读性命名的键,但我还是选择将其删除,因为“night1”和“夜间2”毫无意义的,我:-)
<?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";
?>
我的换档时间是simplier :22:00 - > 07:00 =夜间工作,07:00 - > 22:00 =日常工作
但是我错误地回答Javi R版本,如果轮班像22:30 - > 08:00(结果:每天= 0小时,晚上= 8:30小时)和20:00 - > 10:00(结果:日= 2小时,夜= 9小时),但正确回答时间为23:00 - > 07:00结果:日= 0小时,夜= 7小时)。
对不起,关于这个答案。我没有足够的代表,但我需要得到它的工作。
源数据的格式是什么?它是数据库表行还是另一个? – 2011-05-26 10:10:36