Home >Backend Development >PHP Tutorial >How to use PHP to write employee attendance data verification program?
How to use PHP to write an employee attendance data verification program?
With the development of technology, more and more companies are beginning to use electronic attendance systems to manage employee attendance. When developing an electronic attendance system, the employee attendance data verification program is a very important part. The following will introduce how to use PHP to write an employee attendance data verification program, and attach specific code examples.
The following is an example employee attendance data table structure:
CREATE TABLE employee_attendance ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, employee_id INT NOT NULL, attendance_date DATE NOT NULL, start_time TIME NOT NULL, end_time TIME NOT NULL );
When writing an employee attendance data verification program, we can use PHP's MySQLi extension to connect to the database and write related functions to store and retrieve data.
The following is an example function to insert employee attendance records into the database:
<?php function addEmployeeAttendance($name, $employeeId, $attendanceDate, $startTime, $endTime) { $conn = new mysqli("localhost", "username", "password", "database"); if ($conn->connect_error) { die("连接数据库失败: " . $conn->connect_error); } $sql = "INSERT INTO employee_attendance (name, employee_id, attendance_date, start_time, end_time) VALUES ('$name', $employeeId, '$attendanceDate', '$startTime', '$endTime')"; if ($conn->query($sql) === TRUE) { echo "员工考勤记录插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); } ?>
The following is an example function to verify employee attendance time:
<?php function validateEmployeeAttendance($employeeId, $attendanceDate, $startTime, $endTime) { // 获取员工的出勤规则,这里可以根据实际情况从数据库中获取或手动定义 // 员工的上班时间和下班时间规定 $workStartTime = "09:00:00"; $workEndTime = "18:00:00"; // 检查上班时间是否符合要求 if ($startTime < $workStartTime) { echo "上班时间早于规定时间"; return false; } // 检查下班时间是否符合要求 if ($endTime > $workEndTime) { echo "下班时间晚于规定时间"; return false; } // 所有验证通过 echo "考勤时间验证通过"; return true; } ?>
The following is a sample code to obtain employee attendance data and call the verification function to verify the data:
<?php // 获取员工的考勤数据 $employeeId = 12345; $attendanceDate = '2022-01-01'; $startTime = '08:30:00'; $endTime = '18:30:00'; // 调用验证函数进行数据的验证 validateEmployeeAttendance($employeeId, $attendanceDate, $startTime, $endTime); ?>
The above is a brief introduction to using PHP to write employee attendance data verification program and sample code. In actual development, we can conduct more detailed verification and processing of employee attendance data based on specific needs and business rules. Through reasonable verification procedures, we can improve the accuracy and reliability of attendance data and provide effective support for the company's management work.
The above is the detailed content of How to use PHP to write employee attendance data verification program?. For more information, please follow other related articles on the PHP Chinese website!