我有几个员工的手写时间表,并希望编写一个简单的脚本来计算每个员工的总工时。
我创造的作品除了当班次越过午夜到另一天时。 IE:上午9点至晚上7点返回10小时,但晚上11点至早上7点返回16小时。
我想这可能是因为我只传递时间信息而不是日期信息,所以dateTime类(正确地)让人感到困惑。
我的问题是,有没有办法轻松计算两次交叉到另一天时的差异(无需指定日期)?
表格:
<form id="timeForm" class="form-horizontal" role="form">
<div class="form-group">
<label for="empName" class="col-sm-2 control-label">Name</label>
<div class="col-sm-6 col-md-6 col-lg-6">
<input type="text" class="form-control" id="empName" name="empName" placeholder="Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-md-3 col-lg-3 col-sm-offset-8 col-md-offset-8 col-lg-offset-8 ">
<div class="checkbox">
<label>
<input class="checkbox1" type="checkbox" id="selectall" value="y"> Select All?
</label>
</div>
</div>
</div>
<?php
foreach (range(1, 7) as $number) {
?>
<div class="form-group">
<label for="Day1" class="col-sm-2 col-md-2 col-lg-2 control-label">Day <?php echo $number;?> :</label>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="time" class="form-control" name="day<?php echo $number;?>_start" id="day<?php echo $number;?>_start" placeholder="start time">
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="time" class="form-control" name="day<?php echo $number;?>_finish" id="day<?php echo $number;?>_finish" placeholder="finish time">
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<div class="checkbox">
<label>
<input class="checkbox1" type="checkbox" name="day<?php echo $number;?>_NL" value="y"> No Lunch?
</label>
</div>
</div>
</div>
<?php
}
?>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-2 col-md-2 col-lg-2">
<button id="calculate" type="submit" class="btn btn-default">Calculate</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2">
<button id="clear" type="submit" class="btn btn-default">Clear</button>
</div>
</div>
</form>
<div id="total">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"> </script>
<script>
$(document).ready(function() {
$('#clear').on('click', function(e){
e.preventDefault();
$('#timeForm').trigger("reset");
});
$('#calculate').on('click', function(e){
e.preventDefault();
//$('#calculate').fadeOut(300);
$.ajax({
type: "POST",
url: "timesheet.php",
data: jQuery("#timeForm").serialize(),
cache: false,
success: function(data){
$('#total').append("<p>" + data + "</p>");
//$('#timeForm')[0].reset();
/*if json obj. alert(JSON.stringify(data));*/
}
})
});
$(function() {
var availableTags = [
"George","Brian","Craig","Bruce","Amy","Stew","Cora","Annette","Derek D","James","Patty","Jessica","Derek"
];
$( "#empName" ).autocomplete({
source: availableTags
});
});
$('#selectall').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
处理表单的php:
<?php
$totalHours= '';
foreach (range(1, 7) as $number) {
$hours = '';
$hour = '';
$minute = '';
$starttime = '';
$finishtime = '';
if(!empty($_POST["day{$number}_start"]) && !empty($_POST["day{$number}_finish"]) ){
$starttime = $_POST["day{$number}_start"];
$finishtime = $_POST["day{$number}_finish"];
$from = new DateTime($_POST["day{$number}_start"]);
$to = new DateTime($_POST["day{$number}_finish"]);
//var_dump($from == $to);
//var_dump($from < $to);
//var_dump($from > $to);
if($_POST["day{$number}_NL"] != 'y') {
$from->add(new DateInterval('PT30M'));
}
$hours = $from->diff($to)->format('%H:%I'); //
//debug
//echo $hours . " - ";
list($hour,$minute) = explode(":",$hours);
$minuteF = $minute / 60;
//echo " - " . $minuteF;
$totalHours += $hour + $minuteF;
}
}
$totalHours = round($totalHours,2);
echo $_POST['empName'] ." - " . $totalHours;
//debug
//print_r($_POST);
?>
答案 0 :(得分:0)
只需检查结束时间是否在开始时间之前。如果是这样,那就是第二天就这样添加一天:
$from = new DateTime($_POST["day{$number}_start"]);
$to = new DateTime($_POST["day{$number}_finish"]);
if ($to < $from) {
$to->modify('+1 day'); // or $to->add(new DateInterval('P1D'));
}