Kubes
Kubes

Reputation: 37

PHP Page That Prevents Submission based on time/date

I have a page for my students to login for attendance, really simple, just a name and student id box and it clocks them for the day with a time recording and all that. However, I am trying to implement a 'lock out' system so that they cannot mark attendance outside of class time. The days are Tuesday and Thursday from 11-1215. I can get the time lockout to work or not work if the time is right, but they days are not working, and I don't really know why. Any help would be appreciated.

if (date('G')<=11 || date('G')>= 12 && date('N') !=3 || date('N') !=5)
{
header ('Location: /noattend.php');
exit();
}

Upvotes: 0

Views: 44

Answers (2)

esqew
esqew

Reputation: 44714

I'd recommend brushing up on how Operator Precedence is performed in PHP. In this specific scenario, your conditions will be evaluated from left-to-right, which probably isn't what you intended.

Instead, group your conditions appropriately to meet your logical criteria (and change the logical OR for the day of the week to a logical AND, thanks @enhzflep):

if ((date('G')<=11 || date('G')>= 12) && (date('N') !=2 && date('N') !=4))
{
    header ('Location: /noattend.php');
    exit();
}

Upvotes: 2

rept1d
rept1d

Reputation: 31

N gives you an ISO-8601 numeric representation of the day of the week meaning that the first day of the week is Monday. So Tuesday is 2 and Thursday is 4.

And as @esqew mentioned you have a problem with logic operators as well.

Upvotes: 1

Related Questions