how to substrac two fields from the same column in sql server using php calculation

StackOverflow https://stackoverflow.com/questions/22471003

  •  16-06-2023
  •  | 
  •  

Вопрос

i have SQL server database , and I'm using php to connect to it and using queries. i've read two records from the database for one employee. and i need them to calculate the works hours to the employee. but i couldn't figure how to select to fields from the same column to the same employee. as in the picture:

E003 2014-03-17 08:10:12 2221

E003 2014-03-17 10:05:02 2222

E003 2014-03-17 12:15:20 2221

E003 2014-03-17 15:03:30 2222

the 2221 is the entring door, and the 2222 is the exit door. i want to select the time from the 2222 and substract them to have the works hours for the employee. can you help me with that pleas, this is my code:

<?php

$host = "servername\instancename";
$connectionInfo = array( "Database"=>"AccessCard", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}
echo "</br>"; 

/**********************************************/
$FRISTsql = "SELECT [EmpID],[DATE],[TIME],[OBJECT] FROM [AccessCard].[dbo].[TimeAtt] WHERE [EmpID]='E003'";
$Fstmt = sqlsrv_query( $conn, $FRISTsql );
if( $Fstmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}
while( $Frow = sqlsrv_fetch_array( $Fstmt, SQLSRV_FETCH_ASSOC) ) {

      $R1 = $Frow['EmpID'];
      $R2 = $Frow['DATE']->format('Y-m-d');
      $R3 = $Frow['TIME']->format('H:i:s');
      $R4 = $Frow['OBJECT'];

     ECHO $R1." &nbsp&nbsp";
     ECHO $R2." &nbsp&nbsp&nbsp";
     ECHO $R3." &nbsp&nbsp&nbsp";
     ECHO $R4."</BR> ";

}
sqlsrv_free_stmt( $Fstmt);
Это было полезно?

Решение

For the given results you can do the following. But extending for 4000 records, you may need to look for some query manipulation.

Plz, check the date comparison part as i may not be correct by the PHP syntax and function. But this can be used as a general concept.

 <?php

    $host = "servername\instancename";
    $connectionInfo = array( "Database"=>"AccessCard", "UID"=>"", "PWD"=>"");
    $conn = sqlsrv_connect( $host, $connectionInfo);
    if( $conn === false ) {
        die( print_r( sqlsrv_errors(), true));
    }
    echo "</br>"; 

    /**********************************************/
    $FRISTsql = "SELECT [EmpID],[DATE],[TIME],[OBJECT] FROM [AccessCard].[dbo].[TimeAtt] WHERE [EmpID]='E003' order by [DATE],[TIME]";
    $Fstmt = sqlsrv_query( $conn, $FRISTsql );
    if( $Fstmt === false) {
        die( print_r( sqlsrv_errors(), true) );
    }
else {
$enterTime; $exitTime;
    while( $Frow = sqlsrv_fetch_array( $Fstmt, SQLSRV_FETCH_ASSOC) ) {

          $R1 = $Frow['EmpID'];
          $R2 = $Frow['DATE']->format('Y-m-d');
          $R3 = $Frow['TIME']->format('H:i:s');
          $R4 = $Frow['OBJECT'];
    if($enterTime == null && $R4 == 2001)
    {
             $enterTime = $R3;
     }
    if($exitTime == null && $R4 == 2002)
    {
             $exitTime = $R3;
     }
    else if($R3 < $enterTime && $R4 == 2001)
    {
            $enterTime = $R3;
    }
    else if($R3 > $exitTime && $R4 == 2002)
    {
            $exitTime = $R3;
    }
         ECHO $R1." &nbsp&nbsp";
         ECHO $R2." &nbsp&nbsp&nbsp";
         ECHO $R3." &nbsp&nbsp&nbsp";
         ECHO $R4."</BR> ";

    }
ECHO "User:".$R1." for date".$R2." worked for ".($exitTime -  $enterTime);
}
    sqlsrv_free_stmt( $Fstmt);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top