Find Grade From Student's Graduation Date

Can somebody teach me how to write or use a function that will find a student’s current grade based on their year of graduation?

For example I know that a student will graduate in 2012. How can I get my code to print out “11th Grade?”

(the year of graduation is entered into the database, but the current grade is not)

Thanks.

This isn’t too great, but should give you an idea:

function currentGrade($graduationDate)
	{
		$currentGradYear = "2011";
		
		$currentGrade = $graduationDate-$currentGradYear;
		$currentGrade = 12-$currentGrade;
		
		return $currentGrade." grade.";
	}

Basically even though it’s 2010, the years graduation is 2011, so I set a variable to 2011 (Could be done without inputting 2011).

Take the example of 2010, it takes 2012-the current year, leaving us with 1 year left to go, subtract 1 from 12 grades in total, returning 11.

Thanks a ton.

That is a good start and gives me something to play with.

The only question now is how do I figure out what the current graduation year would be because that value won’t be input anywhere in the the database I’m working with.

I don’t understand, if you’re running this script in your program, then everytime it’s called it’s using the variable set within the function, so you should have complete control over what $currentGradYear is equal to.

Anywho, a little modification (has comments to understand):


## -- Takes the graduation year and returns what
## -- grade the student is in currently.
## -- Based on a 12 grade system.
function currentGrade($graduationDate)
	{
		## -- Sets current month equal to the number of the month
		$currentMonth = date("n");
		
		## -- Sets current graduation year to the current year,
			## -- HOWEVER: If it is after june (6), it adds one to the year.
		$currentGradYear = ($currentMonth>6) ? date("Y")+1 : date("Y");
		
		## -- Formulates their current grade is the currentGradYear subtracted
			## -- from their graduation date.
                        ## -- Then subtracts that from 12 since there are only 12 grades
		$currentGrade = $graduationDate-$currentGradYear;
		$currentGrade = 12-$currentGrade;
		
		## -- Returns their current grade concatenated with " grade."
		return $currentGrade." grade.";
	}

Perfect.

The addition of finding the current month and adding one to the year was the missing piece.

Thanks. I really appreciate it.