php date_diff 如何只加那几天的小时数
php date_diff how to add up only that days hours
我正在做一个程序,你插入 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>";
?>
你的代码比较长,我没有数据来测试。所以我做了一个片段,基本上涵盖了你应该使用的逻辑。
// 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);
我正在做一个程序,你插入 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>";
?>
你的代码比较长,我没有数据来测试。所以我做了一个片段,基本上涵盖了你应该使用的逻辑。
// 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);