I have a PHP script that would insert an attendance of the student. I want to insert in the attendance status, if the time() is between 5AM to 7:30AM then my compareTime function should return P or Present and if time() is between 7:31 and 8:30 then the function should return L or Late. I can't seem to get the proper return value with this comparison between time. I badly need help.
Here is my code:
function compareTime($time,$grade){
if ($grade == "H1" || $grade == "H2" || $grade == "H3" || $grade == "H4" || $grade == "S1" || $grade == "S2") {
if ($time > strtotime('07:30:00') && $time < strtotime('8:30:00')){
return "L";
} else if ($time > strtotime('05:00:00') && $time <= strtotime('7:30:00')){
return "P";
}
} else {
if ($time > strtotime('07:15:00') && $time < strtotime('8:30:00')){
return "L";
} else if ($time > strtotime('05:00:00') && $time <= strtotime('7:30:00')){
return "P";
}
}
Insert Code:
function recordAttendance($conn, $sid, $glid, $scid){
$sql = mysqli_query($conn, "INSERT INTO tbl_attendance(student_id, gradeLevel_id, section_id, date, arrival_time, status) VALUES ('".$sid."', '".$glid."', '".$scid."', CURDATE(), CURTIME(), '".compareTime(time(),$glid)."')")or die(mysqli_error());
UPDATE: I have fixed it out. The reason why the function does not return the exact value is because of the timezone nothing being set. Therefore, whenever I get date('Y-m-d H:i:s') it does not match the date and time of my machine. By adding date_default_timezone_set('Asia/Singapore'); fixed my problem. Thanks everyone who helped!