Php - find out what school year he is

I am trying to write a script that will work in the current school year.

The academic year begins on August 1 of each year.

How can I determine in which school year we are based on the current date.

those. July 31, 2012 (20120631) will work as 2011/2012

August 13, 2012 (20120801) will work as 2012/2013

At the moment, this is what I have, but it is not very good, because I do not want to determine the dates, and it does not return the correct school year only from the originally set $ academ_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 = '';}
+3
source share
3 answers
$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);
+7
source

Datetime. DateTime , (, /) ..

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);
}
+3

This is a little more than others, but the idea is the same. I did this on a page that I could try / check, so this is a form instead of a function. several modifications, and you have 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 ...

0
source

Source: https://habr.com/ru/post/1016554/


All Articles