Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param int $emp_id Numeric employee ID, e.g. 156
- * @param string $date The date as 10 character string e.g. "2017-03-16"
- * @return int The working time, in seconds. e.g. 8 hours = 28800 seconds.
- */
- function get_working_hours( $emp_id, $date ) {}
- $sql = sprintf(
- "SELECT punch_date FROM `%s` WHERE emp_id = %d AND punch_date LIKE '%s %%'",
- 'table_name',
- (int) $emp_id,
- mysqli_real_escape_string( substr( $date, 0, 10 ) )
- );
- // Result SQL looks like:
- // SELECT punch_date FROM `table` WHERE emp_id = 156 AND punch_date LIKE '2017-03-16 %'
- // Get the results from DB, I assume you use mysqli:
- $result = mysqli_query( $db_link, $sql );
- if ( mysqli_num_rows( $result ) != 2 ) {
- // either invalid punches (too many or only 1), or not punched at all.
- return 0;
- }
- $item1 = mysqli_fetch_row( $result );
- $item2 = mysqli_fetch_row( $result );
- // Convert the date string to a numeric timestamp:
- $time1 = strtotime( $item1[0] );
- $time2 = strtotime( $item2[0] );
- // Calculate and return the difference:
- return abs( $time1 - time2 ); // Return value is the working time in SECONDS!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement