0

I am preparing a small php attendance script which will record the time and date of given user.

I am trying to display a message that if person is LATE after given time php to display a message you are late

$CURRENTTIME = new DateTime($data['current_time']);
$CURRENTTIME = $CURRENTTIME->format('H:i:s');
$OFFICETIME  = new DateTime('10:20:00');
$OFFICETIME = $OFFICETIME->format('H:i:s');

var_dump($CURRENTTIME);
var_dump($OFFICETIME);

if ($CURRENTTIM > $OFFICETIME) {
   echo 'you are late tody';
}  else {
   echo 'Thank you for being on time';
}

also i wanted to learn the code if i need a result in between time? let say i need to display result if employee is in between 10:00:00 - 10:30:00

Hooked
  • 84,485
  • 43
  • 192
  • 261

1 Answers1

4

DateTime objects are comparable so no need to compare their formatted strings. Just compare the objects themselves.

    $CURRENTTIME = new DateTime($data['current_time']);
    $OFFICETIME  = new DateTime('10:20:00');

    if ($CURRENTTIME  > $OFFICETIME) {
       echo 'you are late tody';
    }  else {
       echo 'Thank you for being on time';
   }

See this answer for how to see if a time is between two times. It's pretty much the same concept.

Sidenote: There was a typo in your code. $CURRENTTIM which should read as $CURRENTTIME.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496