date関数の練習がてら、カレンダー作りました。
定数つけといたので、そこのパスを変更してコピペすれば使えます。
なんとか動きますが、ごり押しな部分がかなりあります。
今度はこれを改造して、スケジュール帳とか作ってみたいなぁとか思ってます。
例外上手に使えたらとか、メソッドもっとスマートにしたいとか、色々思うとこはありますが、
そういうのも次の機会までに上手になってたらいいなぁって思います。
class Main_Bord { protected $tougetu; protected $tougetu_day; protected $prev; protected $next; private $flg; const path = "/test/calender_class.php"; //ここのパスを変更すれば使えます。 function __construct() { ini_set("output_buffering", "On"); ini_set("output_handler", "mb_output_handler"); ini_set("default_charset", "UTF-8"); mb_language("Japanese"); mb_internal_encoding ("UTF-8"); mb_http_input("auto"); mb_http_output("UTF-8"); mb_detect_order("auto"); mb_substitute_character("none"); ob_start("mb_output_handler"); header("Content-Type: text/html; charset=UTF-8"); session_start(); } public function main($flg="") { $this->f_check($flg); if(empty($flg)) { $this->tougetu = date('Y-m'); $this->get_Year(substr($this->tougetu,0,4)); $this->calender($this->tougetu); } else if((isset($flg["next"])) && ($this->flg)) { $this->tougetu = $flg["next"]; $this->get_Year(substr($this->tougetu,0,4)); $this->calender($this->tougetu); } else if((isset($flg["prev"])) && ($this->flg)) { $this->tougetu = $flg["prev"]; $this->get_Year(substr($this->tougetu,0,4)); $this->calender($this->tougetu); } else { echo "変なフォーマットが入りました。"; } } private function f_check($flg) { if(isset($flg["next"])) { $f_flg = sprintf('%04d-%02d',substr($flg["next"],0,4),substr($flg["next"],5,7)) === '0000-00'; (!$f_flg) ? $this->flg = true : $this->flg = false; } else if(isset($flg["prev"])) { $f_flg = sprintf('%04d-%02d',substr($flg["prev"],0,4),substr($flg["prev"],5,7)) === '0000-00'; (!$f_flg) ? $this->flg = true : $this->flg = false; } } private function get_Year($year) { if($year == date('Y')) { $this->next = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)+1, 1, date("Y"))); $this->prev = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)-1, 1, date("Y"))); $this->tougetu_day = date("t", mktime(0, 0, 0, substr($this->tougetu,5,7), 1, date("Y"))); $this->get_calheader(); } else if($year > date('Y')) { $i = $year - date('Y'); $this->next = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)+1, 1, date("Y")+$i)); $this->prev = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)-1, 1, date("Y")+$i)); $this->tougetu_day = date("t", mktime(0, 0, 0, substr($this->tougetu,5,7), 1, date("Y")+$i)); $this->get_calheader(); } else if($year < date('Y')) { $i = date('Y') - $year; $this->next = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)+1, 1, date("Y")-$i)); $this->prev = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)-1, 1, date("Y")-$i)); $this->tougetu_day = date("t", mktime(0, 0, 0, substr($this->tougetu,5,7), 1, date("Y")-$i)); $this->get_calheader(); } } private function get_calheader() { echo '<style> #calhead { width:500px; overflow:hidden; text-align:left; list-style:none; } #calhead > li { padding-left:75px; float:left; } </style>'.PHP_EOL; echo '<ul id="calhead">'.PHP_EOL; echo '<li>'.PHP_EOL.'<a href="'.self::path.'?next='.$this->next.'">翌月</a>'.PHP_EOL.'</li>'.PHP_EOL; echo '<li>'.$this->tougetu.'</li>'.PHP_EOL; echo '<li>'.PHP_EOL.'<a href="'.self::path.'?prev='.$this->prev.'">先月</a>'.PHP_EOL.'</li>'.PHP_EOL; echo '</ul>'.PHP_EOL; } private function calender($str) { if($str == date("Y-m")) { $days = array(date("t",time()),date("Y-m")); $this->calout($days); } else if (($str == $this->tougetu) && ($str > date("Y-m"))) { $days = array($this->tougetu_day,$this->tougetu); $this->calout($days); return $this->tougetu = $this->next; } else if (($str == $this->tougetu) && ($str < date("Y-m"))) { $days = array($this->tougetu_day,$this->tougetu); $this->calout($days); return $this->tougetu = $this->prev; } } private function calout($days) { if(is_array($days)) { //var_dump($this->tougetu); $youbi = array('日','月','火','水','木','金','土'); if($days[1] === date('Y-m')) { $nen = date('Y'); $tuki = date('m'); $start_w = date("w", mktime(0, 0, 0,$tuki,1, $nen)); } else if($days[1] === $this->tougetu) { $nen = substr($this->tougetu,0,4); $tuki = substr($this->tougetu,5,7); $start_w = date("w", mktime(0, 0, 0,$tuki,1, $nen)); } else if($days[1] === $this->prev) { $nen = substr($this->prev,0,4); $tuki = substr($this->prev,5,7); $start_w = date("w", mktime(0, 0, 0,$tuki,1, $nen)); } echo '<style> #calcon_youbi { overflow:hidden; list-style:none; width:500px; } #calcon_youbi > li { float:left; width:71px; padding:10px 0px 10px 0px; } #calcon_date { overflow:hidden; width:500px; list-style:none; } #calcon_date > li { float:left; width:71px; padding:10px 0px 10px 0px; } </style>'.PHP_EOL; echo '<ul id="calcon_youbi">'.PHP_EOL; for($c = 0; $c < count($youbi); $c++) { ($c >= 40) ? exit : true; echo '<li>'.$youbi[$c].'</li>'.PHP_EOL; } echo '</ul>'.PHP_EOL; echo '<ul id="calcon_date">'.PHP_EOL; for($i = 1; $i <= $start_w; $i++) { ($i >= 40) ? exit : true; echo '<li> </li>'.PHP_EOL; } for($n = 1; $n <= $days[0]; $n++) { ($n >= 40) ? exit : true; echo '<li>'.$n.'</li>'.PHP_EOL; } echo '</ul>'; } } } $bord = new Main_Bord; if(empty($_GET)) { $bord->main(); } else { $bord->main($_GET); }