duanjianshen4871 2015-07-16 11:20 采纳率: 100%
浏览 41
已采纳

查找日期的日期范围

How would you solve the problem below?

On a given date, I'd like to find the range of an academic year. The academic years always start on the first of September and lasts until the last of August. Thus, for today (2015-07-16) I would like the function return the dates 2014-09-01 and 2015-08-31. If I were to run the same function in three months (2015-10-16) I'd like to get 2015-09-01 and 2016-08-31 returned.

Thank you,

/Thomas

-- edit requested by @anant-kumar-singh --

An example function in PHP:

function getAcademicYear($date) {
    // Logic code...
    return array($dateStart, $dateEnd);
}

Calling getAcademicYear('2015-07-16'); should return the array, ['2014-09-01','2015-08-31']

And calling getAcademicYear('2015-10-16'); should return the array, ['2015-09-01','2016-08-31']

-- edit --

function findAcademicYear($date) {
    $dateTime = new DateTime($date);
    $dateTime->sub(new DateInterval("P8M"));
    return(array(
        "startDate" => date("Y-m-d", strtotime($dateTime->format("Y")."-08-31 + 1 day")),
        "endDate" => ($dateTime->format("Y") + 1)."-08-31")
        );
}
  • 写回答

1条回答 默认 最新

  • doufu1504 2015-07-16 11:23
    关注

    I would use the following logic: Subtract 8 months from the date, extract the year, and use that for the specification of the year:

    select date(concat(year(date_sub(date, interval 8 month)), '-09-01')) as academic_start,
           date(concat(1 + year(date_sub(date, interval 8 month)), '-08-31')) as academic_end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”