【日付セット】翌営業日 – その2 (paizaランク B 相当) 解答例 – PHP編【paiza】
【日付セット】 > 翌営業日 – その2 (paizaランク B 相当)
※リンク先へ移動する為には「paiza」へのログインが必要です。
前回とは違い祝日も休みとカウントされる問題です。日本の祝日を配列化するほうが面倒くさかった(;^ω^)
目次
解答例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php $input = explode(" ",trim(fgets(STDIN))); $y = 2019; $m = $input[0]; $d = $input[1]; $w = ucfirst(strtolower($input[2])); $array = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; $holiday = array( "1/1", "1/14", "2/11", "3/21", "4/29", "4/30", "5/1", "5/2", "5/3", "5/4", "5/5", "5/6", "7/15", "8/11", "8/12", "9/16", "9/23", "10/14", "10/22", "11/03", "11/04", "11/23", ); $oneday = 86400; $word = $y."/".$m."/".$d; $ans = strtotime($word); $nextday = $ans + $oneday; $next = date("n/j",$nextday)."/".date("w",$nextday); $test = explode("/",$next); $m = $test[0]; $d = $test[1]; $w = $array[$test[2]]; $test1 = $m."/".$d; $test2 = $y."/".$m."/".$d; $judge = in_array($test1,$holiday,true); while($judge || $w == "Sat" || $w == "Sun"){ $ans = strtotime($test2); $nextday = $ans + $oneday; $next = date("n/j",$nextday)."/".$array[date("w",$nextday)]; $test = explode("/",$next); $m = $test[0]; $d = $test[1]; $w = $test[2]; $test1 = $m."/".$d; $test2 = $y."/".$m."/".$d; $judge = in_array($test1,$holiday,true); } echo $m."月".$d."日"; ?> |
解答方針
前回は土日を進めるだけで良かったので、単純なタイムタンプの増加だけで処理が完了しました。しかし今回は対象月日が土曜日、日曜日、そして祝日かを判定しながら処理を進める必要があります。祝日と土曜日がくっつくこともあり、単純なタイムスタンプの増減だけでは再現できませんからね((+_+))
対象月日と曜日を取得し、日本の祝日を配列化する
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
$input = explode(" ",trim(fgets(STDIN))); $y = 2019; //閏年じゃない年を設定 $m = $input[0]; //対象月 $d = $input[1]; //対象日 $w = ucfirst(strtolower($input[2])); //取得した曜日の頭文字だけ大文字、他を小文字に変換する $array = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; //曜日配列作成 $holiday = array( //日本の祝日配列化する "1/1", "1/14", "2/11", "3/21", "4/29", "4/30", "5/1", "5/2", "5/3", "5/4", "5/5", "5/6", "7/15", "8/11", "8/12", "9/16", "9/23", "10/14", "10/22", "11/03", "11/04", "11/23", ); $oneday = 86400; //一日分のタイムスタンプ |
対象月日から一日分進め、情報を取得する
|
1 2 3 4 5 6 7 8 9 |
$word = $y."/".$m."/".$d; //検査月日を〇年△月◇日という文字列にする $ans = strtotime($word); //検査文字列をタイムスタンプに変換 $nextday = $ans + $oneday; //「$nextday」に一日分進めたタイムスタンプを格納する $next = date("n/j",$nextday)."/".date("w",$nextday); //検査文字列から一日進めた月日を「△(月)/◇日/曜日(配列キー)」という形にする $test = explode("/",$next); //「$nextを"/"区切りで分割」 $m = $test[0]; //月 $d = $test[1]; //日 $w = $array[$test[2]]; //曜日 |
進めた月日を判定し、土、日、祝日なら更に一日分進めるという処理を繰り返す
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$test1 = $m."/".$d; //検査対象から一日後を 「(月)/(日)」 という文字列にする $test2 = $y."/".$m."/".$d; //検査対象から一日後を 「(年)/(月)/(日)」 という文字列にする $judge = in_array($test1,$holiday,true); //祝日かどうかを判定する while($judge || $w == "Sat" || $w == "Sun"){ //祝日、土曜日、日曜日のいずれかなら繰り返し処理 $ans = strtotime($test2); //「$test2」をタイムスタンプに変換 $nextday = $ans + $oneday; //「$ans」を一日分進め「$nextday」に格納する $next = date("n/j",$nextday)."/".$array[date("w",$nextday)]; //「$nextday」を「(月)/(日)/(曜日)」という文字列にし「$next」に格納する $test = explode("/",$next); $m = $test[0]; $d = $test[1]; $w = $test[2]; $test1 = $m."/".$d; $test2 = $y."/".$m."/".$d; $judge = in_array($test1,$holiday,true); } echo $m."月".$d."日"; |

