0

我的案子需要你的帮助。

如何使用条件计算总加班时间?例子 :

使用输入类型,

OT From <input type="text" name="ot_from">
OT To <input type="text" name="ot_to">
Total Hours <input type="text" name="total_hours">

工作日:08.00 - 17.00(正常工作日) 如果我工作到 19.00,应该算我加班 2 小时。

在我的规则中,从 18.00 到 18.30 不计算加班时间,因为那是休息时间。所以我的总加班时间应该是 1.5 小时而不是 2 小时。

有人可以给我一个解决方案吗?感谢你的帮助。

谢谢你。大卫

4

2 回答 2

1

这是你应该做的:

1 将小时和分钟拆分为总分钟数。

str = "1:23" or other time value

time = parseInt(str.split(":")[0], 10)*60+parseInt(str.split(":")[1], 10)

2 从开始时间中减去结束时间 - 这将为您提供最后时间。

3 要将分钟转换为小时,请执行以下操作:

minutes = time%60

hours = parseInt(time/60, 10)

您实际上并不需要 PHP - 只要您不希望将数据存储在数据库或任何东西中,这一切都可以使用 Javascript 完成。

于 2012-06-07T09:38:46.017 回答
0

如果要确保用户的输入始终相等,则应将输入字段替换为选择框。然后,您可以为小时制作一个选择框,为分钟制作一个选择框。

要计算工作时间,您可以进行如下基本计算:

<?php
// Vars of the POST form
$otfrom_hours   = $_POST['ot_from_hours'];
$otfrom_minutes = $_POST['ot_from_minutes'];
$otto_hours     = $_POST['ot_to_hours'];
$otto_minutes   = $_POST['ot_to_minutes'];

// Make a full string for strtotime
$otfrom_string  = $otfrom_hours.":".$otfrom_minutes;
$otto_string    = $otto_hours.":".$otto_minutes;

// Basic calculation
$start          = strtotime($otfrom_string);
$end            = strtotime($otto_string);
$elapsed        = $end - $start;
echo date("H:i", $elapsed);
?>

希望它会有用!

于 2012-06-07T09:46:31.333 回答