根据今天的PHP日期下周

时间:2013-02-04 07:59:57

标签: php date

我想在下周展示,如果它在星期四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;

1 个答案:

答案 0 :(得分:1)

即使你说你只是测试星期四13:00之前或之后的日期,你的if语句还有3个brances。

似乎+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);