php

php용 달력소스

javackr 2025. 2. 18. 11:24

<?php
// 현재 연도와 월을 가져옴
$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
$month = isset($_GET['month']) ? (int)$_GET['month'] : date('m');

// 이전/다음 달 계산
$prev_month = $month - 1;
$next_month = $month + 1;
$prev_year = $year;
$next_year = $year;

if ($prev_month == 0) {
    $prev_month = 12;
    $prev_year--;
}
if ($next_month == 13) {
    $next_month = 1;
    $next_year++;
}

// 달력 데이터 계산
$first_day = strtotime("$year-$month-01");
$days_in_month = date('t', $first_day);
$start_day = date('w', $first_day);
$weeks = ceil(($days_in_month + $start_day) / 7);

// 달력 출력
echo "<h2>{$year}년 {$month}월</h2>";
echo "<a href='?year={$prev_year}&month={$prev_month}'>이전</a> | ";
echo "<a href='?year={$next_year}&month={$next_month}'>다음</a>";

echo "<table border='1' style='width:100%; text-align:center;'>";
echo "<tr><th>일</th><th>월</th><th>화</th><th>수</th><th>목</th><th>금</th><th>토</th></tr>";

$day = 1;
for ($i = 0; $i < $weeks; $i++) {
    echo "<tr>";
    for ($j = 0; $j < 7; $j++) {
        if ($i === 0 && $j < $start_day || $day > $days_in_month) {
            echo "<td></td>";
        } else {
            echo "<td>{$day}</td>";
            $day++;
        }
    }
    echo "</tr>";
}

echo "</table>";
?>

'php' 카테고리의 다른 글

초간편 레이어팝업 오른쪽하단 고정  (1) 2025.02.11
로또번호가 용지별로 나오게  (0) 2025.02.11