Codded
Codded

Reputation: 1256

Php - work out what academic year it is

I am trying to write a script that would work out the current academic year.

Academic year starts on Aug 1st every year.

How can i work out which Academic year we are in based on the current date.

ie 31st July 2012 (20120631) would work out as 2011/2012

13th Aug 2012 (20120801) would work out as 2012/2013

At the moment this is what I have, but its not very good as i dont want to define the dates and it does not return the correct academic year just the originally defined$academic_start_date.

function check_in_range($start_date, $end_date, $date_from_user)
{
  // Convert to timestamp
  $start_ts = strtotime($start_date);
  $end_ts = strtotime($end_date);
  $user_ts = strtotime($date_from_user);

  // Check that user date is between start & end
  return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}


$academic_start_date = '20110801';
$academic_end_date = '20120731';
$startdate = '20120813';

$acyear_check = check_in_range($academic_start_date, $academic_end_date, $startdate);
if ($acyear_check == 1) { $acyear = $academic_start_date;}
else { $acyear = '';}

Upvotes: 3

Views: 4190

Answers (4)

Viktor Matushevskyi
Viktor Matushevskyi

Reputation: 744

Usually you need to get just one value and not 2001/2002 and make this function public and static, so

public static function getAcademicYear()
{
    $now = new DateTime();

    $year = $now->format('Y');
    return ($now->format('m') < 8) ? $year - 1 : $year;
}

then you could reuse it anywhere, for example $academicYear = ClassName::getAcademicYear();

Upvotes: 0

Silvertiger
Silvertiger

Reputation: 1680

This is a little longer than others but the idea is the same .. i made it into a page i could tast/validate with so it's a form instead of a function. a few modifications and you've got a model.

<?PHP
if (!isset($_REQUEST['start']) || empty($_REQUEST['start'])) {
    echo "Enter a date to check for.<br>\n";
} else {
    $start = $_REQUEST['start'];
    $startYear = (int)substr($start,0,4);
    $start_date = date("M d, Y", mktime(0,0,0,substr($start,4,2),substr($start,6,2),substr($start,0,4)));
    $ac_start_date = date("M d, Y", mktime(0,0,0,'8','1',$startYear));
    if ($start_date < $ac_start_date) {
        $AC_Year = ($startYear-1)."/".($startYear);
    } else {
        $AC_Year = ($startYear)." / ".($startYear+1);
    }
    echo "Academic Year for ".$start." = ".$AC_Year.".<br>\n<br>\n";
}
?>
<form name="aychk">
    <div style="float:left;">
        Student start date
    </div>
    <div style="float:left;">
        <input name="start" type="text" placeholder="YYYYMMDD" />
    </div>
    <div style="clear:both;"></div>
    <input type="submit" value="Check">
</form>

but I think it works...

Upvotes: 0

RiaD
RiaD

Reputation: 47620

$time = ??;// here you put timestamp, it also may be strtotime(smth);

$year = date('Y', $time);
if(date('n', $time) < 8)
     $ayear = ($year - 1).'/'.$year;
else
    $ayear = ($year).'/'.($year + 1);

For your format:

$datestr = 'YYYYmmdd';
$year = substr($datestr, 0, 4);
if(intval(substr($datestr,4,2)) < 8)
     $ayear = ($year - 1).'/'.$year;
else
    $ayear = ($year).'/'.($year + 1);

Upvotes: 8

WayneC
WayneC

Reputation: 5740

I would use Datetime objects instead. Much better to pass DateTime objects around in your code, stops any confusion with date formats (eg US/UK) etc.

function academicYear(DateTime $userDate) {
    $currentYear = $userDate->format('Y');
    $cutoff = new DateTime($userDate->format('Y') . '/07/31 23:59:59');
    if ($userDate < $cutoff) {
        return ($currentYear-1) . '/' . $currentYear;
    }
    return $currentYear . '/' . ($currentYear+1);
}

Upvotes: 6

Related Questions