TIP게시판

제목 달력 Data를 배열로 가져오기 helper
글쓴이 SADBLUE 작성시각 2009/10/29 23:16:48
댓글 : 6 추천 : 0 스크랩 : 0 조회수 : 14640   RSS
이 헬퍼는.
달력에 보여지는 모양대로 2차원 배열로 data를 만드는 것에 있습니다.
이전달 뒷부분과 다음달 앞부분 까지 채워서 만들어줍니다.

<?
	function getCalendarArray(&$pController)
	{
		$pController->load->helper("date");
		
		$stWeek = array();
		$stMonth = array();
		
		$nCurrentYear = mdate("%Y", now());
		$nCurrentMonth = mdate("%n", now());
		$nCurrentDate = mdate("%d", now());

		$nStartDate = 1;

		$nStartDay = mdate("%w", strtotime(sprintf("%s%02s01", $nCurrentYear, $nCurrentMonth)));
		$nLastDate = days_in_month($nCurrentMonth, $nCurrentYear);

		if ($nCurrentMonth == 1)
			$nPrevMonthLastDate = days_in_month(12, ($nCurrentYear-1));
		else
			$nPrevMonthLastDate = days_in_month(($nCurrentMonth-1), $nCurrentYear);

		for($i=0 ; $i<$nStartDay ; ++$i)
			array_push($stWeek, ($nPrevMonthLastDate - $nStartDay + $i + 1));
		
		for($i=$nStartDay ; $i<7 ; ++$i)
			array_push($stWeek, $nStartDate++);
		
		array_push($stMonth, $stWeek);	// 맨 첫주 완성
		unset($stWeek);
		$stWeek = array();

		for($i=$nStartDate ; $i<=$nLastDate ; ++$i)
		{
			if (sizeof($stWeek) == 7)
			{
				array_push($stMonth, $stWeek);

				unset($stWeek);
				$stWeek = array();
			}
			array_push($stWeek, $i);
		}

		$nFillCount = sizeof($stWeek);
		for($i=1 ; $i<=7-$nFillCount; ++$i)
			array_push($stWeek, $i);

		array_push($stMonth, $stWeek);
		unset($stWeek);
		
		return $stMonth;
	}
?>

controller를 인자로 넘기는 것은
CI에서 제공하는 date helper를 사용하는데 없을 경우를 확인 하기 힘드니...
그냥 한번 더 load를 호출 하기 위함입니다.
없으면 load될테고...있으면 알아서 넘어가겠지요.~_~;

 다음글 달력 Data를 배열로 가져오기 js버전. (2)
 이전글 공개된 포럼 소스를 lighttpd에 설치할 때 .ht... (5)

댓글

SADBLUE / 2009/11/01 14:41:53 / 추천 0
버그가 있어 수정 하였습니다.~_~;
ci_beginne / 2009/11/03 13:40:36 / 추천 0

SADBLUE님 사용법은 어떻게 되죠?

SADBLUE / 2009/11/03 22:37:12 / 추천 0
일단 위의 소스를 helper로 만드셔서 application/helper밑에 저장 하시구요.

$this->load->helper(_저장하신helper이름_);
$data = getCalendarArray($this);
이렇게 쓰시면 data에 2차원 배열로 넘어 옵니다.
이건 지금 무조건 오늘을 기준으로 배열을 만드는지라.
특정한 달의 달력 data를 가져오게 하시려면 년.월을 인자로 받아서 수정 하시면 될겁니다.



ci_beginne / 2009/11/03 23:27:16 / 추천 0
SADBLUE님 친절한 답변에 감사드립니다.^^
변종원(웅파) / 2009/11/04 00:32:54 / 추천 0
적용해봐야겠네요. 감사합니다.
SADBLUE / 2009/11/07 18:57:23 / 추천 0
아유 이누무 버그.;; 민망하여라.; 마지막 주 채우는데 버그가 있었습니다.-_-;수정 다시 했습니다.;