2016-04-08 19 views
1

私は週刊誌を簡単に作成しようとしています。ここに私のコードは次のとおりです。php前の翌週選択した日付に基づいて

$daterange = new DatePeriod(
    new DateTime('2016-04-08'), 
    new DateInterval('P1D'), 
    new DateTime(date('Y-m-d',strtotime ('1 week' , strtotime ('2016-04-08')))) 
); 

そして、単純なループ:完全に正常に動作しますが、私は必要なのから始まる日付を表示することです

08 - Fri 
09 - Sat 
10 - Sun 
11 - Mon 
12 - Tue 
13 - Wed 
14 - Thu 

foreach ($daterange as $k => $row) { 
    echo $row->format('d') ." - " . $row->format('D') . "<br>"; 
} 

のようなものを作成します日曜日または月曜日、週の最初の曜日に基づいています。望ましい結果は次のようになります。

10 - Sun 
11 - Mon 
12 - Tue 
13 - Wed 
14 - Thu 
15 - Fri 
16 - Sat 

答えて

0

ので、ここで別のソリューションです:

 
// The question is ambiguous on this, so we'll use a 
// variable to configure the preferred first weekday 
$firstWeekDay = 'Sunday'; 

$dateTime  = new DateTime(); 
// If today isn't the first week day, get the last one 
if ($dateTime->format('l') !== $firstWeekDay) 
{ 
     $dateTime->modify("last {$firstWeekDay}"); 
} 

$period = new DatePeriod(
     $dateTime, 
     new DateInterval('P1D'), 
     6 // Just tell PHP to create 6 (7 - the 1 start day) more dates 
); 
+0

を「非整形式の数値が遭遇した」取得しています完璧。ありがとうございました。 – Alko

1

あなたはこのようなもの使用することができます:あなたは何が必要に応じて、next Monday/last Sunday/next Sundaylast Mondayを置き換えることができ

date('Y-m-d', strtotime('last Monday', '2016-04-08')); 

を。これにより、現在の1日の前/次の曜日が表示されます。この日から7日間隔を取得することができます。これは、一般的にDateTimestrtotime()date()などを混合することが悪いと考えられている

+0

は、私はそれを試してみましたが、私は – Alko

関連する問題