简体   繁体   English

从日期中查找日期范围

[英]Find a date range from a date

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. 学年总是从9月1日开始,一直持续到8月底。 Thus, for today (2015-07-16) I would like the function return the dates 2014-09-01 and 2015-08-31. 因此,对于今天(2015-07-16),我希望函数返回日期2014-09-01和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. 如果我要在三个月内(2015-10-16)运行相同的功能,我想得到2015-09-01和2016-08-31。

Thank you, 谢谢,

/Thomas /托马斯

-- edit requested by @anant-kumar-singh -- -由@ anant-kumar-singh请求编辑-

An example function in PHP: PHP中的示例函数:

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

Calling getAcademicYear('2015-07-16'); 调用getAcademicYear('2015-07-16'); should return the array, ['2014-09-01','2015-08-31'] 应该返回数组, ['2014-09-01','2015-08-31']

And calling getAcademicYear('2015-10-16'); 并调用getAcademicYear('2015-10-16'); should return the array, ['2015-09-01','2016-08-31'] 应该返回数组, ['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")
        );
}

I would use the following logic: Subtract 8 months from the date, extract the year, and use that for the specification of the year: 我将使用以下逻辑:从日期中减去8个月,提取年份,并将其用于年份的说明:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM