DateTime获取上周的日期

时间:2016-01-08 15:31:37

标签: php function date datetime

我正在尝试开始&从上周开始的结束日期,它无法获取数据。在其他几周工作,所以我的猜测是由于新的一年它的错误。 代码:

function getStartAndEndDateOfWeek($week, $year) {
    $dto = new DateTime();
    $dto->setISODate($year, $week);
    $ret['week_start'] = $dto->format('Y-m-d');
    $dto->modify('+7 days');//1 day extra for BETWEEN query fix
    $ret['week_end'] = $dto->format('Y-m-d');
    return $ret;
}

//last week
        $lastWeek = (new DateTime('now'))->sub(new DateInterval('P7D'))->format('W');
        $year = (new DateTime('now'))->sub(new DateInterval('P7D'))->format('Y');
        $dateRange = getStartAndEndDateOfWeek($lastWeek, $year);

0 个答案:

没有答案