Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. /**
  2. * @param int $emp_id Numeric employee ID, e.g. 156
  3. * @param string $date The date as 10 character string e.g. "2017-03-16"
  4. * @return int The working time, in seconds. e.g. 8 hours = 28800 seconds.
  5. */
  6. function get_working_hours( $emp_id, $date ) {}
  7.  
  8. $sql = sprintf(
  9. "SELECT punch_date FROM `%s` WHERE emp_id = %d AND punch_date LIKE '%s %%'",
  10. 'table_name',
  11. (int) $emp_id,
  12. mysqli_real_escape_string( substr( $date, 0, 10 ) )
  13. );
  14. // Result SQL looks like:
  15. // SELECT punch_date FROM `table` WHERE emp_id = 156 AND punch_date LIKE '2017-03-16 %'
  16.  
  17. // Get the results from DB, I assume you use mysqli:
  18. $result = mysqli_query( $db_link, $sql );
  19.  
  20. if ( mysqli_num_rows( $result ) != 2 ) {
  21. // either invalid punches (too many or only 1), or not punched at all.
  22. return 0;
  23. }
  24.  
  25. $item1 = mysqli_fetch_row( $result );
  26. $item2 = mysqli_fetch_row( $result );
  27.  
  28. // Convert the date string to a numeric timestamp:
  29. $time1 = strtotime( $item1[0] );
  30. $time2 = strtotime( $item2[0] );
  31.  
  32. // Calculate and return the difference:
  33. return abs( $time1 - time2 ); // Return value is the working time in SECONDS!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement