0

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!

khrisdaniels
  • 79
  • 10
  • Why not just use `case` in your `select` query later and return `p` when in the right range or `l` when outside? – chris85 Aug 14 '17 at 13:40
  • What's the output of $time in the function? Maybe the format is different? Another option is that the comparison is a "string" comparison which will cause unplanned results. – Ofir Baruch Aug 14 '17 at 13:45
  • @chris85 We could use that. But what we want is to store the returned value in our status column for future purposes. – khrisdaniels Aug 14 '17 at 13:47
  • @OfirBaruch it's in this format 1502675521. That is also why i used strtotime in my pre defined time to match it. Am I wrong? – khrisdaniels Aug 14 '17 at 13:50
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Aug 14 '17 at 16:45

2 Answers2

0

For all strtotime add date also

   strtotime('2017-08-14 07:30:00')
K.B
  • 885
  • 5
  • 10
0

Instead of strtotime('05:00:00') use strtotime('today')+86400*5; (timestamp of 00:00 today + 5 hours)

Tharaka Wijebandara
  • 7,955
  • 1
  • 28
  • 49
Mihzam
  • 1
  • Is it possible to compare time in this format 'H:i:s'? – khrisdaniels Aug 14 '17 at 14:05
  • try this - strtotime('today 07:30:00'); "today %your time%" – Mihzam Aug 14 '17 at 14:10
  • strtotime('today 07:30:00'); doesn't work. Do you have any solution for this? All I really want is to return "P" if the insert time of data is between 5AM to 7:30AM. This problem have cracked my head for days. – khrisdaniels Aug 14 '17 at 14:53
  • I noticed that when I echo date('Y-m-d H:i:s') it does not equal or match the date and time on my machine. Why is that? – khrisdaniels Aug 14 '17 at 15:26