2014-12-30 4 views
0

나는보고 대답을 찾지 못했습니다.PHP 달력이 건너 뜁니다.

나는 php 달력을 가지고 있으며 다음 달 또는 지난 달에 feb로 이동하면 feb를 건너 뛰고 feb는 건너 뛰고 feb는 skb로 이동합니다. 4 년마다이 작업을 수행하므로 윤년과 관련이 있지만 문제를 발견하지 못하는 것 같습니다.

<html> 
<head> 
<script> 
function goLastMonth(month, year){ 
if (month == 1) { 
--year; 
month = 13; 
} 
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+(month-1)+"&year="+year; 
} 

function goNextMonth(month, year){ 
if (month == 12) { 
++year; 
month = 0; 
} 
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+(month+1)+"&year="+year; 
} 



</script> 




</head> 

<body> 
<?php 
if (isset($_GET['day'])){ 
$day = $_GET['day']; 
}else{ 
$day = date("j"); 
} 
if (isset($_GET['month'])){ 
$month = $_GET['month']; 
}else{ 
$month = date("n"); 
} 
if (isset($_GET['year'])){ 
$year = $_GET['year']; 
}else{ 
$year = date("Y"); 
} 

// calender variable // 
$currentTimeStamp = strtotime("$year-$month-$day"); 
$monthName = date("F", $currentTimeStamp); 
$numDays = date("t", $currentTimeStamp); 
$counter = 0; 


?> 

<table border='1'> 
<tr> 
<td><input style='width:50px;' type='button' value='<' name='previousbutton' onclick="goLastMonth(<?php echo $month.",".$year?>)"></td> 
<td colspan='5' align='center'> <?php echo $monthName.", ".$year; ?></td> 
<td><input style='width:50px;' type='button' value='>' name='nextbutton' onclick="goNextMonth(<?php echo $month.",".$year?>)"></td> 
<td></td> 
</tr> 
<tr> 
<td width='50px' align='center'>D</td> 
<td width='50px' align='center'>L</td> 
<td width='50px' align='center'>M</td> 
<td width='50px' align='center'>M</td> 
<td width='50px' align='center'>J</td> 
<td width='50px' align='center'>V</td> 
<td width='50px' align='center'>S</td> 
</tr> 
<?php 
echo "<tr>"; 
for($i = 1; $i < $numDays+1; $i++, $counter++) { 
$timeStamp = strtotime("$year-$month-$i"); 
if ($i == 1) { 
$firstDay = date("w", $timeStamp); 
for ($j = 0; $j < $firstDay; $j++, $counter++) { 
// blank space // 
echo "<td>&nbsp;</td>"; 
} 
} 
if ($counter % 7 == 0 && $counter != 0){ 
echo "</tr><tr>"; 
} 
echo "<td align='center'>".$i."</td>"; 
} 
echo "</tr>"; 
?> 

</table> 

</body> 

+1

당신의'goLastMonth'와'goNextMonth'는 꽤 생생 해 보입니다. 왜 당신은 돌아갈 때 "1 월 1 일"에서 "13 월"으로 갈 것이지만 앞으로 갈 때 "12 월"에서 "0 월"으로 갈 것입니다. – meagar

+0

DateTime 객체를 살펴 보시지 않겠습니까? http://php.net/manual/en/datetime.add.php –

답변

0
if (isset($_GET['day'])) { 
    $day = $_GET['day']; 
} else { 
    $day = date("j"); 
} 

을 ...

$currentTimeStamp = strtotime("$year-$month-$day"); 

명시적인 요일을 지정하지 않으면 오늘 날짜가 표시됩니다. 맞춰봐, 오늘은 30 일이야. 2 월 30 일은 없습니다. 그런 다음 모든 날짜 계산을 기반으로합니다.

특정 월의 타임 스탬프를 만든 다음 다음 달까지 반복하려면 항상 해당 월의 시작을 가져갑니다. 1 월 1 일 + 1 달은 의미가 있으며 1 월 30 일 + 1 달은 의미가 없습니다.

1

나는 문제가 PHP 달 1 월을 취급하면서 자바 스크립트가 0 개월 1 월을 취급한다는 것입니다 의심 1.

난 당신이 것 같은 느낌 : 여기

코드입니다 이걸 뒤집었다.

자바 스크립트 또는 PHP에서 월을 받고 있습니까? 자바 스크립트라면 12 개월을 가져서는 안됩니다. 두 경우 모두 13 개월이 없어야합니다.

$monthmonth이 아니기 때문에 나는 자바 스크립트에서 온다고 가정합니다. 이 경우, 나는이 같은 변경 것이라고 생각 :

function goLastMonth(month, year){ 
    if (month == 0) { 
     --year; 
     month = 11; 
} 

...

function goNextMonth(month, year){ 
    if (month == 11) { 
     ++year; 
     month = 0; 
} 

...

관련 문제