0

我想在下周展示,如果它在星期四 13:00 之后它应该返回 +2 周(下周之后的一周)。如果不是周末,此代码可以正常工作。它在星期五之后给出错误的开始日期。什么会导致这个?

$current_time = strtotime('now');

if ($current_time < strtotime('thursday this week 13:00')) {
    $week_start = date('d/m/Y', strtotime('this week next monday', strtotime(date('d-m-Y'))));
    $week_end = date('d/m/Y', strtotime('next week next sunday', strtotime(date('d-m-Y'))));
} else {
    if (date('N') > 5) {
        $week_start = date('d/m/Y', strtotime('+2 week next monday', strtotime(date('d-m-Y'))));
    } else {
        $week_start = date('d/m/Y', strtotime('next week next monday', strtotime(date('d-m-Y'))));
    }

    $week_end = date('d/m/Y', strtotime('+2 week next sunday', strtotime(date('d-m-Y')))); 
} 

return $week_start." - ".$week_end;
4

1 回答 1

1

即使您说您只测试日期是否在星期四 13:00 之前或之后,您的 if 语句也有 3 个分支。

似乎+2 week next monday应该是next week next monday,而读取的行next week next monday可能永远不会执行。

代码也可以这样简化:

if ($current_time < strtotime('thursday this week 13:00')){
    $week_start = strtotime('monday next week');
}else{
    $week_start = strtotime('monday +1 week');
}

$week_end = strtotime('next sunday', $week_start);

return date("Y/m/d", $week_start)." - ".date("Y/m/d", $week_end); 
于 2013-02-04T08:56:31.670 回答