Jump to content

[SOLVED] Arbitrary Beginning of Year


coolego1

Recommended Posts

Hello,

I am working on a site for a school which is database driven with MySQL and using PHP.  The school calendar obviously does not follow a typical annual calendar, but I would like to be able to perform certain functions on the dates in the database such that they refer to the beginning of September rather than the beginning of January.  For instance, if I know that a student's graduating year is 2010, I know that they are a freshman this year because they will graduate in June in 3 years.  However, in December, the same 2010 date would give me that they are one year before freshman because the difference in years is 4.  Can I write a function to perform calculations based on the current school year?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/51402-solved-arbitrary-beginning-of-year/
Share on other sites

I know that i need to use month/year calculations,  but I guess I'm kind of looking for an easy way to do that.  I would like to be able to quickly compare and decide what "year" it's in terms of 05-06 or 06-07 or 07-08.  I guess I'm looking for some guidance as to how to effectively implement something like that...

I think this may do it

<?php
function syear_diff($graduate, $date) 
{
     list($gy,$gm) = explode('-', $graduate);
     list($dy, $dm) = explode('-', $date);
     /**
     * if month < 9, school year is prev year
     */
     if ($gm < 9) --$gy;
     if ($dm < 9) --$dy;
     return $gy - $dy;
}

$grad_in = '2010-06';
$date = '2006-12';

echo syear_diff($grad_in, $date);
?>

This is what I ended up with:

<?php
function showclass ($gradyear) {
	$thisgradyear = date('Y');
	if (date('n') > 6) { // this is after August, but before January
		$thisgradyear = date('Y') + 1;
	}
	if ($gradyear == $thisgradyear) {
		$class = "Senior";
	} else if ($gradyear == $thisgradyear+1) {
		$class = "Junior";
	} else if ($gradyear == $thisgradyear+2) {
		$class = "Sophomore";
	} else if ($gradyear == $thisgradyear+3) {
		$class = "Freshman";
	} else {
		$class = "None";
	}
	return $class;
}
?>

Thanks for the suggestion though!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.