PHP日期范围根据今天的日期

时间:2010-08-01 00:21:20

标签: php date-range

有人能想出一个更好的方法,在循环中写出来并得到相同的结果吗?

$today = date('l');

    if($today == 'Wednesday'){
        $min = date('l-m-d-y');
        $max = date('l-m-d-y', strtotime('+4 days'));
    }else if($today == 'Thursday'){
        $min = date('l-m-d-y', strtotime('-1 days'));
        $max = date('l-m-d-y', strtotime('+3 days'));
    }else if($today == 'Friday'){
        $min = date('l-m-d-y', strtotime('-2 days'));
        $max = date('l-m-d-y', strtotime('+2 days'));
    }else if($today == 'Saturday'){
        $min = date('l-m-d-y', strtotime('-2 days'));
        $max = date('l-m-d-y', strtotime('+1 days'));
    }else if($today == 'Sunday'){
        $min = date('l-m-d-y', strtotime('-3 days'));
        $max = date('l-m-d-y');
    }

    echo $min . ' - ' . $max;

2 个答案:

答案 0 :(得分:3)

我假设你想要在星期六的分钟-3和星期日的-4分。无论如何,这是个主意:

$weekday = date("w");
if ($weekday == 0)
    $weekday = 7;

if ($weekday >= 3) {
    $min = date('l-m-d-y',
        strtotime(($weekday==3?"+0":(3-$weekday))." days");
    $max = date('l-m-d-y',
        strtotime("+".(7-$weekday)." days");
}

答案 1 :(得分:1)

可以将它存储在一个数组中,其中以日为键,+ / - x天为值。