테스트 사이트 - 개발 중인 베타 버전입니다

php 간단한 달력 채택완료

찬돌이 4년 전 조회 2,331

$start_youil 의 출력  4를 이용하여 

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

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

</p>

<p>$year = date('Y');

$month = date('n');

list($last_day, $start_youil) = preg_split('[ ]',date('t w',mktime(0,0,1,$month,1,$year)));</p>

<p>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>";</p>

<p>for($i=1; $i<=$last_day; $i++) {

    echo "<td> {$i} </td>";

    if($i%7==0) {

        echo "</tr><tr>";

    }

}</p>

<p>echo "</table>";</p>

<p>

[ 현 코드 출력 결과 ]

7 월

일월화수목금토

1234567

891011121314

15161718192021

22232425262728

293031

[ 원하는 결과 ]

일월화수목금토

            123

45678910

11121314151617

18192021222324

25262728293031

입니다. 

 

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

답변 3개

채택된 답변
+20 포인트
e
4년 전

</p>

<p>echo "<tr align=center>";</p>

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

<p>   echo "<td></td>";</p>

<p>}

for($i=1; $i<=$last_day; $i++) {

    echo "<td> {$i} </td>";

    if($i%7==0) {

        echo "</tr><tr>";

    }

}</p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

찬돌이
4년 전
감사합니다. 아래 결과처럼 밀려만 갑니다.
일 월 화 수 목 금 토
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부터 내려가서 자리를 잡을 수 있는 방법.......
e
eyekiss
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>";
}

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

</p>

<p>$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++;

}</p>

<p>

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

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

찬돌이
4년 전
정말 간단한 방법으로 해결해 주셔서 너무 감사 합니다.

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

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)

 

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

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

로그인 후 평가할 수 있습니다

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

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

로그인