채택완료

php 간단한 달력

2021-07-12 (월) 21:29:23 2,475

$start_youil 의 출력  4를 이용하여 

목요일부터 1이 시작되고 싶은데, 

공간을 남겨두고 밀어서 시작하기 어렵습니다.

Copy
$year = date('Y');
$month = date('n');
list($last_day, $start_youil) = preg_split('[ ]',date('t w',mktime(0,0,1,$month,1,$year)));

echo "<table border=1>";
echo "<tr align=center><td colspan=7>{$month} 월</td></tr>";
echo "<tr align=center><th>일</th><th>월</th><th>화</th><th>수</th><th>목</th><th>금</th><th>토</th></tr>";
echo "<tr align=center>";

for($i=1; $i<=$last_day; $i++) {
    echo "<td> {$i} </td>";
    if($i%7==0) {
        echo "</tr><tr>";
    }
}

echo "</table>";

[ 현 코드 출력 결과 ]

7 월

일월화수목금토

1234567

891011121314

15161718192021

22232425262728

293031

[ 원하는 결과 ]

일월화수목금토

            123

45678910

11121314151617

18192021222324

25262728293031

입니다. 

|

답변 3개 / 댓글 3개

채택된 답변
+20 포인트
Copy
echo "<tr align=center>";

for($i=0; $i<date('w',mktime(0,0,0,$month,1,$year)); $i++) {

   echo "<td></td>";

}
for($i=1; $i<=$last_day; $i++) {
    echo "<td> {$i} </td>";
    if($i%7==0) {
        echo "</tr><tr>";
    }
}

답변에 대한 댓글 2개

감사합니다. 아래 결과처럼 밀려만 갑니다.
일 월 화 수 목 금 토
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
4부터 내려가서 자리를 잡을 수 있는 방법.......
for($i=1; $i<=$last_day; $i++) {
if(date('w',mktime(0,0,0,$month,$i,$year)==0) {
echo "</tr><tr>";
}
echo "<td> {$i} </td>";
}
2021-07-13 (화) 10:08:53
Copy
$j = 1;
for($i = (1 - $start_youil); $i <= $last_day; $i++) {
    echo $i > 0 ? "<td>".$i."</td>" : "<td></td>";
    if($j % 7 == 0) echo "</tr><tr>";
    $j++;
}

이렇게 해 보셔도 될 것 같습니다.

답변에 대한 댓글 1개

정말 간단한 방법으로 해결해 주셔서 너무 감사 합니다.
2021-07-12 (월) 22:48:02

https://www.php.net/manual/en/datetime.format.php

l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
N ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)

오늘이 무슨 요일이냐에 따라서

처음 빈 칸을 만들어 주면 됩니다.

답변을 작성하려면 로그인이 필요합니다.