0

Hello guys so into web page im gettting date/time variable which looks like 2018-12-05 07:19:54 and I am getting IN or EXIT. Here is cauculating if person is coming in or going out and how long he has been in and I need seperate every entrence from 2018-12-05 00:01 to 2018-12-05 23:59:59.

Btw the file looks like this

Does anyone know how to do math? Example (2018-12-05 07:19:54)-(2018-12-05 07:50:05) = 31min in the end it would be at work he was 31min and cauculate that untill the day ends. Anyone knows any ideas how to cauculate that sort of kind stuff?

Meister96Fels
  • 508
  • 1
  • 8
  • 26

3 Answers3

0

You can using date_diff function in PHP

For example:

$date1=date_create("2018-12-05  07:19:54");
$date2=date_create("2018-12-05  07:50:05");
$diff=date_diff($date1,$date2);
echo $diff->format("'%i Minute %s Seconds'");

For running code check here

Check here for complete format response

dianeryanto
  • 600
  • 2
  • 5
  • 18
0

PHP's Date Time class has method called diff. This method would return a DateInterval object, you can easily retrieve values of year, month, day... from that object.

Haris
  • 368
  • 5
  • 15
0

Soo this is what i have done and it cauculates the time from in to exit

<?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;

  $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 "</td><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><td>";
      echo "</td><td>";
      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 Valandos %i Minutes %s Sekundes'");
         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 "</table>";
  

?>