Php - work out what academic year it is
datetime, php
Solution
$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);
Problem
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 = '';} ```