回答編集履歴

3

調整

2019/05/27 01:19

投稿

yambejp
yambejp

スコア114968

test CHANGED
@@ -107,3 +107,71 @@
107
107
  print_r($list);
108
108
 
109
109
  ```
110
+
111
+
112
+
113
+ # 第n週のx曜日
114
+
115
+ ```PHP
116
+
117
+ $start="2019-01-01";
118
+
119
+ $end ="2019-12-31";
120
+
121
+ $d_start =strtotime($start." first day of 0 month");
122
+
123
+ $d_end =strtotime($end." first day of 1 month");
124
+
125
+ $day_of_week=["sunday","monday","tueday","wednesday","thuesday","friday","saturday"];
126
+
127
+ $target=[["sunday",6]];
128
+
129
+ $list=[];
130
+
131
+ $d=$d_start;
132
+
133
+ $c=0;
134
+
135
+ while($d<$d_end){
136
+
137
+ $w=(date("w",$d));
138
+
139
+ foreach($target as $val){
140
+
141
+ $offset=array_search($val[0],$day_of_week)<$w?1:0;
142
+
143
+ $day=date("Y-m-d",strtotime(date("Y-m-d",$d).sprintf(" first %s of 0 month +%d week",$val[0],$val[1]-1-$offset)));
144
+
145
+ if(date("Y-m",$d)==substr($day,0,7)) $list[]=$day;
146
+
147
+ }
148
+
149
+ $d=strtotime(date("Y-m-d",$d)." + 1 month");
150
+
151
+ }
152
+
153
+ sort($list);
154
+
155
+ $list=array_filter($list,function($x) use($start,$end){
156
+
157
+ return $x>=$start and $x<=$end;
158
+
159
+ });
160
+
161
+ print_r($list);
162
+
163
+ ```
164
+
165
+ 上記で
166
+
167
+ ```PHP
168
+
169
+ $start="2018-01-01";
170
+
171
+ $end ="2050-07-25";
172
+
173
+ $target=[["monday",1],["tuesday",2],["thursday",3]];
174
+
175
+ ```
176
+
177
+ とすれば最初の命題にも合致すると思います

2

tuiki

2019/05/27 01:19

投稿

yambejp
yambejp

スコア114968

test CHANGED
@@ -49,3 +49,61 @@
49
49
 
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ # 追記
56
+
57
+ 任意の曜日とn番目を指定
58
+
59
+ ```PHP
60
+
61
+ $start="2018-01-01";
62
+
63
+ $end ="2050-07-25";
64
+
65
+
66
+
67
+ $d_start =strtotime($start." first day of 0 month");
68
+
69
+ $d_end =strtotime($end." first day of 1 month");
70
+
71
+
72
+
73
+ $target=[["monday",1],["tuesday",2],["thursday",3]];
74
+
75
+ //上記、第1月曜、第2火曜、第3木曜
76
+
77
+
78
+
79
+ $list=[];
80
+
81
+ $d=$d_start;
82
+
83
+ $c=0;
84
+
85
+ while($d<$d_end){
86
+
87
+ foreach($target as $val){
88
+
89
+ $list[]=date("Y-m-d",strtotime(date("Y-m-d",$d).sprintf(" first %s of 0 month +%d week",$val[0],$val[1]-1)));
90
+
91
+ }
92
+
93
+
94
+
95
+ $d=strtotime(date("Y-m-d",$d)." + 1 month");
96
+
97
+ }
98
+
99
+ sort($list);
100
+
101
+ $list=array_filter($list,function($x) use($start,$end){
102
+
103
+ return $x>=$start and $x<=$end;
104
+
105
+ });
106
+
107
+ print_r($list);
108
+
109
+ ```

1

chousei

2019/05/24 04:20

投稿

yambejp
yambejp

スコア114968

test CHANGED
@@ -18,13 +18,9 @@
18
18
 
19
19
 
20
20
 
21
-
22
-
23
21
  $list=[];
24
22
 
25
23
  $d=$d_start;
26
-
27
- $c=0;
28
24
 
29
25
  while($d<$d_end){
30
26