Php - 制定出什么学年 [英] Php - work out what academic year it is

查看:92
本文介绍了Php - 制定出什么学年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



学年每年8月1日开始。



我如何计算出目前日期的学年。



ie
2012年7月31日(20120631) )将作为2011/2012计划。



2012年8月13日(20120801)将作为2012/2013计划。



在这一刻,这是我所拥有的,但它不是很好,因为我不想定义日期,它不会返回正确的学年只是原来定义的$ academic_start_date。

  function check_in_range($ start_date,$ end_date,$ date_from_user)
{
//转换为时间戳
$ start_ts = strtotime($ start_date );
$ end_ts = strtotime($ end_date);
$ user_ts = strtotime($ date_from_user);

//检查用户日期是否在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 ='';}


解决方案

  $ time = ??; //这里你放时间戳,它也可能是strtotime smth); 

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

对于您的格式:

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


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 = '';}

解决方案

$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);

这篇关于Php - 制定出什么学年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆