获取周开始和下一周的结束

时间:2014-02-10 15:28:00

标签: php date

我一直在使用我发现的修改过的功能,但它仍然需要修复它。

public static function getStartAndEndDate($week, $year) {
    $dto = new DateTime();
    $dto->setISODate($year, $week);
    $ret = Array();
    for($i = 0; $i<=9;$i++)
    {
        $ret[$i]['week_start'] = $dto->format('d/m/Y');
        $dto->modify('+6 days');
        $ret[$i]['week_end'] = $dto->format('d/m/Y');
    }
    return $ret;
}

使用

$week_array = class::getStartAndEndDate(date('W'),date('Y'));
echo '<pre>';print_r($week_array);exit;

我明白了:

Array
(
    [0] => Array
        (
            [week_start] => 10/02/2014
            [week_end] => 16/02/2014
        )

    [1] => Array
        (
            [week_start] => 16/02/2014
            [week_end] => 22/02/2014
        )

    [2] => Array
        (
            [week_start] => 22/02/2014
            [week_end] => 28/02/2014
        )

    [3] => Array
        (
            [week_start] => 28/02/2014
            [week_end] => 06/03/2014
        )

    [4] => Array
        (
            [week_start] => 06/03/2014
            [week_end] => 12/03/2014
        )

    [5] => Array
        (
            [week_start] => 12/03/2014
            [week_end] => 18/03/2014
        )

    [6] => Array
        (
            [week_start] => 18/03/2014
            [week_end] => 24/03/2014
        )

    [7] => Array
        (
            [week_start] => 24/03/2014
            [week_end] => 30/03/2014
        )

    [8] => Array
        (
            [week_start] => 30/03/2014
            [week_end] => 05/04/2014
        )

    [9] => Array
        (
            [week_start] => 05/04/2014
            [week_end] => 11/04/2014
        )

)

本周工作(周一在这里),我们得到10/02/2014 - 2014年2月16日,但第二周而不是17/02/2014 - 2014年2月23日实际上是16/02 / 2014 - 22/02/2014。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以像这样添加另一个+1 day

function getStartAndEndDate($week, $year) {
    $dto = new DateTime();
    $dto->setISODate($year, $week);
    $ret = Array();
    for($i = 0; $i<=9;$i++)
    {
        $ret[$i]['week_start'] = $dto->format('d/m/Y');
        $dto->modify('+6 days');
        $ret[$i]['week_end'] = $dto->format('d/m/Y');
        $dto->modify('+1 day');
    }
    return $ret;
}