首页 > 解决方案 > php date_diff 如何只加起来那几天的小时数

问题描述

我正在做一个程序,如果您插入 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>";
		

?>

输出

标签: phphtmldatedatediff

解决方案


你的代码很长,我没有数据来测试它。所以我做了一个片段,基本上涵盖了你应该使用的逻辑。

// 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);

推荐阅读