PHP를 이용하여 간단한 달력 만들기 예제
PHP를 이용하여 간단한 달력 만들기 예제
REDINFO
약 1년전 2023-01-09 01:45:29

PHP로 개발을 하다보면 상당히 많은 라이브러리를 접할 수 있다. 하지만 달력 같은 경우 보통 PHP가 아닌 datepicker 같은 JS라이브러리를 많이 사용하게 되는데 이벤트에 대한 메서드를 완전히 제어하기 어렵기 때문에 별도로 제작해야할때가 있다. 

 

하지만 평소 달력은 보기만 했지 막상 만들려면 조금이나마 공수가 들어가게 된다. 이럴땐 바로 구글링을 해보면 상당히 많은 예제들을 볼 수 가 있는데 여기서는 간단하게 만든 소스이니 원리 이해정도로 참고하는게 좋다. 

 

달력예제 소스 
<?php 
$date = empty($_GET['date']) ? date('Y-m-d') : $_GET['date'];
$firstDate = date('Y-m-01',strtotime($date));
$firstDateWcode = date('w',strtotime($firstDate));
$lastDateDay = date('t', strtotime($date));
$lastDateWcode = date('w',strtotime( date('Y-m-'.$lastDateDay,strtotime($date)) ));

$prevDate = date('Y-m-d',strtotime("-1 month",strtotime($firstDate)));
$nextDate = date('Y-m-d',strtotime("+1 month",strtotime($firstDate)));
$prevLastDateDay = date('t', strtotime($prevDate));

$week = array();
$day = 1;
while(1){
	$subWeek = array();
	for($i=0;$i<7;$i++){
		if( $day > $lastDateDay){ 

			// 마지막 날의 날짜코드가 6이 아니라면, 다음달을 미리 넣어준다.
			if( $lastDateWcode != 6){
				for($si=0;$si < 6-$lastDateWcode; $si++){
					$thisDate = date('Y-m-'.sprintf("%02d",($si+1)),strtotime("+1 month",strtotime($firstDate)));
					$subWeek[] = array('type'=>'next', 'day'=>$si+1,'date'=>$thisDate,'wcode'=>date('w',strtotime($thisDate)));		
				}				
			}
			break; 
		}
		if( $day == 1 && $firstDateWcode != 0){
			for($si=($prevLastDateDay-$firstDateWcode);$si < $prevLastDateDay; $si++){
				$thisDate = date('Y-m-'.sprintf("%02d",($si+1)),strtotime("-1 month",strtotime($firstDate)));
				$subWeek[] = array('type'=>'prev', 'day'=>$si+1,'date'=>$thisDate,'wcode'=>date('w',strtotime($thisDate)));
			}
			$i += $firstDateWcode;
		}

		$thisDate = date('Y-m-'.sprintf("%02d",$day),strtotime($firstDate));
		$subWeek[] = array('type'=>'now', 'day'=>$day,'date'=>$thisDate,'wcode'=>date('w',strtotime($thisDate)));		
		$day++;
	}
	$week[] = $subWeek;
	if( $day > $lastDateDay){ break; }
}

echo '
<body>
	<div class="action">
		<a href="?date='.$prevDate.'">이전달</a>
		<strong>'.$date.'</strong>
		<a href="?date='.$nextDate.'">다음달</a>
	</div>
	<table>
		<tr>
			<th>일</th>
			<th>월</th>
			<th>화</th>
			<th>수</th>
			<th>목</th>
			<th>금</th>
			<th>토</th>
		</tr>
';

foreach($week as $k=>$v){
	echo '<tr>';
	foreach($v as $sk=>$sv){
		echo '<td class="wcode_'.$sv['wcode'].''.($sv['type'] != 'now' ? ' prevnext':'').'" title="'.$sv['date'].'">'.$sv['day'].'</td>';
	}
	echo '</tr>';
}
echo '</body>';
?>
<style>
	body{  width:450px; }
	.action{ text-align:center; margin:10px 0; }
	.action strong{ font-size:45px; }
	.action a{ text-decoration: none; color:#333; }
	h1{ text-align:center; }
	table{ width:100%;}
	table th,td{ width: 14%; text-align:center; }
	table th.wcode_0,table td.wcode_0{ color:red; }
	table th.wcode_6,table td.wcode_6{ color:blue; }
	table td.prevnext{ color:#eee; }
</style>

 

| 결과화면

 

급하게 만들다 보니 예제소스상 정리가 잘 안된것 같은데 이부분은 양해좀 구하며 이번편은 간단하게만 알아보고 다음편에서는 디테일한 달력 만들기 예제를 살펴보도록 하자. 

 

 

이 포스트글이 도움이 되었나요?
6
카테고리 연관글

Comment

댓글작성

0(500) | 1(30)