回答編集履歴
2
厳密比較に修正
answer
CHANGED
@@ -35,6 +35,7 @@
|
|
35
35
|
以下、実装例
|
36
36
|
※もっと簡潔に書けますが、順を追っていけるよう分解しました。
|
37
37
|
```php
|
38
|
+
//1週間の設定配列
|
38
39
|
$weekconfig = [
|
39
40
|
0=>"日",
|
40
41
|
1=>"月",
|
@@ -44,19 +45,22 @@
|
|
44
45
|
5=>"金",
|
45
46
|
6=>"土"
|
46
47
|
];
|
48
|
+
|
49
|
+
//指定日
|
47
50
|
$targetDate = '2019-01-07';
|
48
51
|
//指定日の曜日
|
49
52
|
$targetWeekDay = date("w",strtotime($targetDate));
|
50
53
|
//日曜日の場合は7を代入して調整
|
51
|
-
if($targetWeekDay == 0){
|
54
|
+
if($targetWeekDay === "0"){
|
52
|
-
$targetWeekDay=7;
|
55
|
+
$targetWeekDay = "7";
|
53
56
|
}
|
54
57
|
//指定日を含む週の月曜日を求める
|
55
58
|
$targetSunday = date("Ymd",strtotime("-".$targetWeekDay." day",strtotime($targetDate)));
|
56
59
|
$targetMonday = date("Ymd",strtotime("+1 day",strtotime($targetSunday)));
|
57
60
|
|
61
|
+
//月曜日から7日間加算してそれぞれの日付を出力
|
58
62
|
for($i=0;$i<7;$i++){
|
59
|
-
$w = date("w",strtotime("+{$i} day",strtotime($targetMonday)));
|
63
|
+
$w = date("w",strtotime("+{$i} day",strtotime($targetMonday)));//現在の曜日
|
60
64
|
echo date($weekconfig[$w]."曜日:Y-m-d",strtotime("+{$i} day",strtotime($targetMonday)))."\n";
|
61
65
|
}
|
62
66
|
```
|
1
修正
answer
CHANGED
@@ -8,4 +8,55 @@
|
|
8
8
|
[strtotime()](http://php.net/manual/ja/function.strtotime.php)の第1引数で[timeの書式](http://php.net/manual/ja/datetime.formats.php)を利用します。
|
9
9
|
|
10
10
|
曜日を表示したければwを指定すると日曜日が0として順番に6が土曜日でとれますので、
|
11
|
-
0=>日 ~ 6=>土 のような配列で変換テーブルを作って照合すると良いです。
|
11
|
+
0=>日 ~ 6=>土 のような配列で変換テーブルを作って照合すると良いです。
|
12
|
+
|
13
|
+
# コメントを受けて追記
|
14
|
+
|
15
|
+
> 「2019-01-07」は月曜日ですが、その他の曜日の日付も扱います。
|
16
|
+
指定日付から一週間ではなく、指定した日付が含まれる一週間です。
|
17
|
+
例えば、「2019-01-11」は金曜ですが、こちらの文字列から、下記の7日間分の文字列を得る事が目的です。
|
18
|
+
月曜日:2019-01-07
|
19
|
+
火曜日:2019-01-08
|
20
|
+
水曜日:2019-01-09
|
21
|
+
木曜日:2019-01-10
|
22
|
+
金曜日:2019-01-11
|
23
|
+
土曜日:2019-01-12
|
24
|
+
日曜日:2019-01-13
|
25
|
+
|
26
|
+
考え方。
|
27
|
+
+n day で「その日の何日後」が取得できることは上記で回答したとおり。
|
28
|
+
date("w") で曜日が数値がとれることも既に回答したとおりです。
|
29
|
+
|
30
|
+
「指定日が含まれる月曜日からの1週間」ということは
|
31
|
+
`指定日の曜日から曜日の番号を引いて1を足したものが月曜日`ということになります。
|
32
|
+
つまり、その日を求めて1週間を求めていけば良い訳ですね。
|
33
|
+
※日曜日の場合は0となり+1すると翌週になるので-7しておくなど考慮が必要
|
34
|
+
|
35
|
+
以下、実装例
|
36
|
+
※もっと簡潔に書けますが、順を追っていけるよう分解しました。
|
37
|
+
```php
|
38
|
+
$weekconfig = [
|
39
|
+
0=>"日",
|
40
|
+
1=>"月",
|
41
|
+
2=>"火",
|
42
|
+
3=>"水",
|
43
|
+
4=>"木",
|
44
|
+
5=>"金",
|
45
|
+
6=>"土"
|
46
|
+
];
|
47
|
+
$targetDate = '2019-01-07';
|
48
|
+
//指定日の曜日
|
49
|
+
$targetWeekDay = date("w",strtotime($targetDate));
|
50
|
+
//日曜日の場合は7を代入して調整
|
51
|
+
if($targetWeekDay == 0){
|
52
|
+
$targetWeekDay=7;
|
53
|
+
}
|
54
|
+
//指定日を含む週の月曜日を求める
|
55
|
+
$targetSunday = date("Ymd",strtotime("-".$targetWeekDay." day",strtotime($targetDate)));
|
56
|
+
$targetMonday = date("Ymd",strtotime("+1 day",strtotime($targetSunday)));
|
57
|
+
|
58
|
+
for($i=0;$i<7;$i++){
|
59
|
+
$w = date("w",strtotime("+{$i} day",strtotime($targetMonday)));
|
60
|
+
echo date($weekconfig[$w]."曜日:Y-m-d",strtotime("+{$i} day",strtotime($targetMonday)))."\n";
|
61
|
+
}
|
62
|
+
```
|