【日付セット】カレンダーの表示 (paizaランク B 相当) 解答例 – PHP編【paiza】
【日付セット】 > カレンダーの表示 (paizaランク B 相当)
※リンク先へ移動する為には「paiza」へのログインが必要です。
今までの方法を総動員すれば解けない問題ではないのですが、普通に面倒だし難しいですね(;^ω^)
解説もできる気がしないorz
解答例
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 64 65 66 67 |
<?php $input = explode(" ",trim(fgets(STDIN))); $weekarray = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); $array = array(); $year = $input[0]; $month = $input[1]; $day = 1; $next = $year."/".$month."/".$day; $next = strtotime($next."+1 month"); $next = date("Y/n/j/D",$next); $next1 = explode("/",$next); $nextyear = $next1[0]; $nextmonth = $next1[1]; $nextday = $next1[2]; $nextweek = $next1[3]; $date = $year."/".$month."/".$day; $date = date("Y/n/j/D",strtotime($date)); while($year != $nextyear || $month != $nextmonth || $day != $nextday){ array_push($array,$date); $date = $year."/".$month."/".$day; $date = strtotime($date."+1 day"); $date = date("Y/n/j/D",$date); $test = explode("/",$date); $year = $test[0]; $month = $test[1]; $day = $test[2]; $week = $test[3]; $date = $year."/".$month."/".$day."/".$week; } $ansarray = array(); for($i = 0;$i < 6;$i++){ for($j = 0;$j < 7;$j++){ $sample = explode("/",$array[0]); $year = $sample[0]; $month = $sample[1]; $day = $sample[2]; $week = $sample[3]; if($week == $weekarray[$j]){ $ansarray[$i][$j] = $day; array_shift($array); } else { $ansarray[$i][$j] = " "; } } } for($i = 0;$i < 6;$i++){ for($j = 0;$j < 7;$j++){ if(mb_strlen($ansarray[$i][$j]) == 1){ echo " ".$ansarray[$i][$j]; } elseif(mb_strlen($ansarray[$i][$j]) == 2) { echo $ansarray[$i][$j]; } else { echo " "; } if($j != 6){ echo " "; } } if($j != 5){ echo "\n"; } } ?> |
