0

現在の日付について、前の日曜日から土曜日までの日付範囲を取得する方法が必要です。

たとえば、今日が 8/15 の場合、8/4 ~ 8/10 が必要です。

4

3 に答える 3

0

1. 現在の日付は 8/10 です。日付範囲が 8/4 ~ 8/10 でなければならない場合のコード:

$to = new DateTime('2013-08-10');
$to->modify('-' . (($w = $to->format('w')) != 6 ? $w + 1 : 0) . ' day');
$from = clone $to;
$from->modify('-6 day');

echo $from->format('m/d') . "-" . $to->format('m/d'); # 08/04-08/10

2. 現在の日付は 8/10 です。日付範囲が 7/28-8/03 でなければならない場合のコード:

$to = new DateTime('2013-08-10');
$to->modify('last Saturday');
$from = clone $to;
$from->modify('-6 day');

echo $from->format('m/d') . "-" . $to->format('m/d'); # 07/28-08/03
于 2013-08-15T14:25:09.580 に答える
-4
function getPreviousSundayAndSatruday($today = NULL)
{
    $today = is_null($today) ? time() : $today;

    // If today is Sunday, then find last week
    if(date("w", $today) == 0){
        $saturdayTimeStamp = strtotime("last Saturday");
        $sundayTimeStamp = strtotime("last Sunday");
    }
    // If it is Saturday, check from last Sunday until today
    else if(date("w", $today) == 6){
        $saturdayTimeStamp = strtotime("this Saturday");
        $sundayTimeStamp = strtotime("last Sunday");
    }
    // Else it's a day of the week, so last Saturday to two Sundays before.
    else{
        $saturdayTimeStamp = strtotime("last Saturday");
        $sundayTimeStamp = strtotime("last Sunday - 1 week");
    }

    return array(
        'saturday'  => $saturdayTimeStamp,
        'sunday'    => $sundayTimeStamp
    );
}
于 2013-08-15T14:07:43.657 に答える