我正在做一个程序,如果您插入excel文件,然后选择人员,然后从excel中显示人员何时上班,何时上班。但是现在我需要把同一天的所有时间加起来。 例如,该人在工作中待了2个小时,然后离开,过了一段时间,他又回来了,现在又呆了5个小时,然后又离开了。 因此,现在我需要程序能够识别上一个和下一个日期是否相同。如果它们相同,则将它们加起来。但是问题是如何做到的,如果该人说夜班,可以说他在18:00进去,第二天06:00外出。所以这些时间必须分开。有人有什么想法吗?
到目前为止的代码:
table tr td {
border: 1px solid black;
}
table {
border: 1px solid black;
}
<?php
error_reporting(0); //disable all errors and notices
require_once "Classes/PHPExcel.php";
$chosenPerson = $_GET["dla_darbuotojo_pasirinkimas"];
$tmpfname = "visi.xls";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
$aInT = "Administracija[In]";
$vInT = "Vartai[In]";
$aExT = "Administracija[Exit]";
$vExT = "Vartai[Exit]";
$goingIn = false;
$goingExt = false;
$diffFinall = 0;
$goingInValue = 0;
$goingExtValue = 0;
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
if ($chosenPerson == ($worksheet->getCell('D'.$row)->getValue()) ) {
if (!$goingIn or !$goingExt) {
//checking if the person alredy went in
if ((($worksheet->getCell('G'.$row)->getValue()) == $aInT) or (($worksheet->getCell('G'.$row)->getValue()) == $vInT)) {
//if the person went in
$goingIn = true;
echo "<tr><td>";
$goingInValue = $worksheet->getCell('F'.$row)->getValue();
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><td>";
for ($erow= $row +1; ; $erow++){
if ($chosenPerson == ($worksheet->getCell('D'.$erow)->getValue()) ) {
if ((($worksheet->getCell('G'.$erow)->getValue()) == $aExT) or (($worksheet->getCell('G'.$erow)->getValue()) == $vExT)) {
$goingExtValue = $worksheet->getCell('F'.$erow)->getValue();
$goingExt=true;
echo $worksheet->getCell('D'.$erow)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$erow)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$erow)->getValue();
echo "</td><td>";
$date1=date_create($goingInValue);
$date2=date_create($goingExtValue);
$diff=date_diff($date2,$date1);
echo $diff->format("%h Val %i Min %s Sek");
$diffFinall= $diffFinall + $diff;
echo "</td><tr>";
$goingIn = false;
$goingExt = false;
break;
$row=$erow;
}
}
}
}
}
//echo "<tr><td>";
//echo $worksheet->getCell('D'.$row)->getValue();
//echo "</td><td>";
//echo $worksheet->getCell('F'.$row)->getValue();
//echo "</td><td>";
//echo $worksheet->getCell('G'.$row)->getValue();
//echo "</td><tr>";
}
}
echo "<tr><td>";
echo "Viso:";
echo $diffFinall;
echo "</td></tr>";
echo "</table>";
?>
答案 0 :(得分:0)
您的代码很长,我没有数据可以对其进行测试。因此,我制作了一个片段,基本上涵盖了您应该使用的逻辑。
// Define total hours before.
$totalHours = 0;
// Imaginary loop {
/**
* B and E are th columns
* should be replaced with $worksheet->getCell('B'.$erow)->getValue()
*/
$B = '2018-12-04 07:44:56';
$E = '2018-12-04 18:52:31';
$B = strtotime($B);
$E = strtotime($E);
// this total hours could be part of a multi dimensional array
// it keeps adding up because of this: +=
$totalHours += ($E-$B)/60/60;
// } End Imaginary Loop
echo $totalHours;
您可以使用$chosenPerson
作为数组中的键,对其进行多维命名。像这样:
$totalHours = array();
// in the loop:
if (!isset($totalHours[$chosenPerson]))
$totalHours[$chosenPerson] = ($E-$B)/60/60;
else
$totalHours[$chosenPerson] += ($E-$B)/60/60;
// end of loop
var_dump($totalHours);