coolego1 Posted May 14, 2007 Share Posted May 14, 2007 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 More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 You would have to take month and year into account rather than just the year, Link to comment https://forums.phpfreaks.com/topic/51402-solved-arbitrary-beginning-of-year/#findComment-253153 Share on other sites More sharing options...
coolego1 Posted May 15, 2007 Author Share Posted May 15, 2007 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... Link to comment https://forums.phpfreaks.com/topic/51402-solved-arbitrary-beginning-of-year/#findComment-253159 Share on other sites More sharing options...
marf Posted May 15, 2007 Share Posted May 15, 2007 the date() and mktime() functions will be your friend Link to comment https://forums.phpfreaks.com/topic/51402-solved-arbitrary-beginning-of-year/#findComment-253247 Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 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); ?> Link to comment https://forums.phpfreaks.com/topic/51402-solved-arbitrary-beginning-of-year/#findComment-253394 Share on other sites More sharing options...
coolego1 Posted May 17, 2007 Author Share Posted May 17, 2007 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! Link to comment https://forums.phpfreaks.com/topic/51402-solved-arbitrary-beginning-of-year/#findComment-255918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.