Pergunta

I am working on a school web project and I need to know the what year a particular student is in from his/her unique registration number.

The registration number looks like UXXEEXXXX, where the first two XX's represent the last two digits of the year the user joined the school, EEX represents the department, and the last three XXX's represent the serial number of the student according to when he when he was registered.

For example, in U08EE1117, U represents undergraduate, 08 says the user joined the school in 2008, EE1 indicates the department and the 1 also says the user started from the first year, and 117 means the user is the 117th student registered in the department.

I want to use PHP to get the current level of the user, which is represented as level 100 for the first year, level 200 for the second, up to level 500 for the final year students.

What I did was this:

$yr = ((integer)substr($_SESSION["username"], 1,2));    
$cur = substr(date("Y"), 2, 3);
$level = $cur - $yr;

where $_SESSION["username"] is the registration number.

But I noticed that this does not work for a lot of years. I would like to know the easiest way to go around this. (The registration number works only within 100 years.)

Foi útil?

Solução

I'd say you're off to a good start. All you need to do is fix the wrap-around issue by adding 100 to the difference if it's negative:

$yr = substr($_SESSION["username"], 1, 2);    
$cur = date("y");  // two-digit year
$diff = $cur - $yr;
if ($diff < 0) $diff += 100;

This gives the number of calendar years since the student started, to which you might need to add one if the current school year has already started. Then just multiply by 100 to get the level as you've specified it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top