質問編集履歴
3
誤記
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,58 +13,6 @@
|
|
13
13
|
|
14
14
|
class holiday
|
15
15
|
{
|
16
|
-
|
17
|
-
public function get_public_holiday($from_date, $to_date){
|
18
|
-
|
19
|
-
// APIキー
|
20
|
-
$api_key = 'AIzaSyBkaqOMNOIL5ki_ABoQv77_3EprfaM2aOo';
|
21
|
-
// カレンダーID(Googleが提供する日本の祝日カレンダー)
|
22
|
-
$calendar_id = urlencode('japanese__ja@holiday.calendar.google.com');
|
23
|
-
// 開始日
|
24
|
-
$start = date($from_date . '\T00:00:00\Z');
|
25
|
-
// 終了日
|
26
|
-
$end = date($to_date . '\T00:00:00\Z');
|
27
|
-
|
28
|
-
// Google Calendar API
|
29
|
-
$url = "https://www.googleapis.com/calendar/v3/calendars/" . $calendar_id . "/events?";
|
30
|
-
$query = [
|
31
|
-
'key' => $api_key,
|
32
|
-
'timeMin' => $start,
|
33
|
-
'timeMax' => $end,
|
34
|
-
'maxResults' => 50,
|
35
|
-
'orderBy' => 'startTime',
|
36
|
-
'singleEvents' => 'true'
|
37
|
-
];
|
38
|
-
$results = [];
|
39
|
-
if ($data = file_get_contents($url. http_build_query($query), true)) {
|
40
|
-
$data = json_decode($data);
|
41
|
-
|
42
|
-
// $data->itemには日本の祝日カレンダーの"予定"が入る
|
43
|
-
foreach ($data->items as $row) {
|
44
|
-
// [祝日の日付 => 祝日のタイトル]
|
45
|
-
$results[$row->start->date] = $row->summary;
|
46
|
-
}
|
47
|
-
}
|
48
|
-
return $results;
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
|
-
public function get_weekend($from_date, $to_date){
|
53
|
-
|
54
|
-
$holiday = array();
|
55
|
-
do{
|
56
|
-
$datetime = new DateTime($from_date);
|
57
|
-
if ((int)$datetime->format('w') == 0 || (int)$datetime->format('w') == 6){
|
58
|
-
array_push($holiday, $from_date);
|
59
|
-
}
|
60
|
-
$from_date = date("Y-m-d", strtotime($from_date . "+1 day"));
|
61
|
-
echo $from_date;
|
62
|
-
}while ($from_date <= $to_date);
|
63
|
-
|
64
|
-
return $holiday;
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
16
|
public function get_weekdays($from_date, $to_date)
|
69
17
|
{
|
70
18
|
$days = array();
|
@@ -86,90 +34,4 @@
|
|
86
34
|
}
|
87
35
|
}
|
88
36
|
|
89
|
-
|
90
|
-
```
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
### ソースコード(クラスにしない場合うまくいく)
|
96
|
-
```php
|
97
|
-
$from_date = "2020-08-07";
|
98
|
-
$to_date = "2020-08-18";
|
99
|
-
$from_date2 = "2020-08-07";
|
100
|
-
$to_date2 = "2020-08-18";
|
101
|
-
$from_date3 = "2020-08-07";
|
102
|
-
$to_date3 = "2020-08-18";
|
103
|
-
|
104
|
-
|
105
|
-
$days = array();
|
106
|
-
do{
|
107
|
-
array_push($days, $from_date);
|
108
|
-
$from_date = date("Y-m-d", strtotime($from_date . "+1 day"));
|
109
|
-
}while ($from_date <= $to_date);
|
110
|
-
|
111
|
-
|
112
|
-
$weekend = get_weekend($from_date2, $to_date2);
|
113
|
-
|
114
|
-
$aa = array_values(array_diff($days, $weekend));
|
115
|
-
|
116
|
-
$bb = array_flip(get_public_holiday($from_date3, $to_date3));
|
117
|
-
|
118
|
-
$aa = array_values(array_diff($aa, $bb));
|
119
|
-
|
120
|
-
var_dump($aa);
|
121
|
-
|
122
|
-
|
123
|
-
function get_weekend($from_date, $to_date){
|
124
|
-
|
125
|
-
$holiday = array();
|
126
|
-
do{
|
127
|
-
$datetime = new DateTime($from_date);
|
128
|
-
if ((int)$datetime->format('w') == 0 || (int)$datetime->format('w') == 6){
|
129
|
-
array_push($holiday, $from_date);
|
130
|
-
}
|
131
|
-
if ($from_date < $to_date) {
|
132
|
-
$from_date = date("Y-m-d", strtotime($from_date . "+1 day"));
|
133
|
-
}
|
134
|
-
}while ($from_date != $to_date);
|
135
|
-
|
136
|
-
return $holiday;
|
137
|
-
}
|
138
|
-
|
139
|
-
function get_public_holiday($from_date, $to_date){
|
140
|
-
|
141
|
-
// APIキー
|
142
|
-
$api_key = 'AIzaSyBkaqOMNOIL5ki_ABoQv77_3EprfaM2aOo';
|
143
|
-
// カレンダーID(Googleが提供する日本の祝日カレンダー)
|
144
|
-
$calendar_id = urlencode('japanese__ja@holiday.calendar.google.com');
|
145
|
-
// 開始日
|
146
|
-
$start = date($from_date . '\T00:00:00\Z');
|
147
|
-
// 終了日
|
148
|
-
$end = date($to_date . '\T00:00:00\Z');
|
149
|
-
|
150
|
-
// Google Calendar API
|
151
|
-
$url = "https://www.googleapis.com/calendar/v3/calendars/" . $calendar_id . "/events?";
|
152
|
-
$query = [
|
153
|
-
'key' => $api_key,
|
154
|
-
'timeMin' => $start,
|
155
|
-
'timeMax' => $end,
|
156
|
-
'maxResults' => 50,
|
157
|
-
'orderBy' => 'startTime',
|
158
|
-
'singleEvents' => 'true'
|
159
|
-
];
|
160
|
-
$results = [];
|
161
|
-
if ($data = file_get_contents($url. http_build_query($query), true)) {
|
162
|
-
$data = json_decode($data);
|
163
|
-
|
164
|
-
// $data->itemには日本の祝日カレンダーの"予定"が入る
|
165
|
-
foreach ($data->items as $row) {
|
166
|
-
// [祝日の日付 => 祝日のタイトル]
|
167
|
-
$results[$row->start->date] = $row->summary;
|
168
|
-
}
|
169
|
-
}
|
170
|
-
return $results;
|
171
|
-
}
|
172
|
-
|
173
|
-
|
174
|
-
|
175
37
|
```
|
2
誤記
title
CHANGED
File without changes
|
body
CHANGED
@@ -105,10 +105,8 @@
|
|
105
105
|
$days = array();
|
106
106
|
do{
|
107
107
|
array_push($days, $from_date);
|
108
|
-
if ($from_date < $to_date) {
|
109
|
-
|
108
|
+
$from_date = date("Y-m-d", strtotime($from_date . "+1 day"));
|
110
|
-
}
|
111
|
-
}while ($from_date
|
109
|
+
}while ($from_date <= $to_date);
|
112
110
|
|
113
111
|
|
114
112
|
$weekend = get_weekend($from_date2, $to_date2);
|
1
誤記
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
どうやらget_weekendの土日を判定するところでうまくいっていないのですが、get_weekend単体ではうまくいきます。
|
4
4
|
よろしくお願いします。
|
5
5
|
|
6
|
-
### ソースコード
|
6
|
+
### ソースコード(期待する結果とならない)
|
7
7
|
```php
|
8
8
|
$judge_h = new holiday;
|
9
9
|
var_dump($judge_h->get_weekdays('2020-08-07', '2020-08-17')); // 期待値にならない
|
@@ -87,4 +87,91 @@
|
|
87
87
|
}
|
88
88
|
|
89
89
|
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
### ソースコード(クラスにしない場合うまくいく)
|
96
|
+
```php
|
97
|
+
$from_date = "2020-08-07";
|
98
|
+
$to_date = "2020-08-18";
|
99
|
+
$from_date2 = "2020-08-07";
|
100
|
+
$to_date2 = "2020-08-18";
|
101
|
+
$from_date3 = "2020-08-07";
|
102
|
+
$to_date3 = "2020-08-18";
|
103
|
+
|
104
|
+
|
105
|
+
$days = array();
|
106
|
+
do{
|
107
|
+
array_push($days, $from_date);
|
108
|
+
if ($from_date < $to_date) {
|
109
|
+
$from_date = date("Y-m-d", strtotime($from_date . "+1 day"));
|
110
|
+
}
|
111
|
+
}while ($from_date != $to_date);
|
112
|
+
|
113
|
+
|
114
|
+
$weekend = get_weekend($from_date2, $to_date2);
|
115
|
+
|
116
|
+
$aa = array_values(array_diff($days, $weekend));
|
117
|
+
|
118
|
+
$bb = array_flip(get_public_holiday($from_date3, $to_date3));
|
119
|
+
|
120
|
+
$aa = array_values(array_diff($aa, $bb));
|
121
|
+
|
122
|
+
var_dump($aa);
|
123
|
+
|
124
|
+
|
125
|
+
function get_weekend($from_date, $to_date){
|
126
|
+
|
127
|
+
$holiday = array();
|
128
|
+
do{
|
129
|
+
$datetime = new DateTime($from_date);
|
130
|
+
if ((int)$datetime->format('w') == 0 || (int)$datetime->format('w') == 6){
|
131
|
+
array_push($holiday, $from_date);
|
132
|
+
}
|
133
|
+
if ($from_date < $to_date) {
|
134
|
+
$from_date = date("Y-m-d", strtotime($from_date . "+1 day"));
|
135
|
+
}
|
136
|
+
}while ($from_date != $to_date);
|
137
|
+
|
138
|
+
return $holiday;
|
139
|
+
}
|
140
|
+
|
141
|
+
function get_public_holiday($from_date, $to_date){
|
142
|
+
|
143
|
+
// APIキー
|
144
|
+
$api_key = 'AIzaSyBkaqOMNOIL5ki_ABoQv77_3EprfaM2aOo';
|
145
|
+
// カレンダーID(Googleが提供する日本の祝日カレンダー)
|
146
|
+
$calendar_id = urlencode('japanese__ja@holiday.calendar.google.com');
|
147
|
+
// 開始日
|
148
|
+
$start = date($from_date . '\T00:00:00\Z');
|
149
|
+
// 終了日
|
150
|
+
$end = date($to_date . '\T00:00:00\Z');
|
151
|
+
|
152
|
+
// Google Calendar API
|
153
|
+
$url = "https://www.googleapis.com/calendar/v3/calendars/" . $calendar_id . "/events?";
|
154
|
+
$query = [
|
155
|
+
'key' => $api_key,
|
156
|
+
'timeMin' => $start,
|
157
|
+
'timeMax' => $end,
|
158
|
+
'maxResults' => 50,
|
159
|
+
'orderBy' => 'startTime',
|
160
|
+
'singleEvents' => 'true'
|
161
|
+
];
|
162
|
+
$results = [];
|
163
|
+
if ($data = file_get_contents($url. http_build_query($query), true)) {
|
164
|
+
$data = json_decode($data);
|
165
|
+
|
166
|
+
// $data->itemには日本の祝日カレンダーの"予定"が入る
|
167
|
+
foreach ($data->items as $row) {
|
168
|
+
// [祝日の日付 => 祝日のタイトル]
|
169
|
+
$results[$row->start->date] = $row->summary;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
return $results;
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
90
177
|
```
|