这是我为使用php和html生成日历而创建的代码。但是这个代码在2月份没有工作,它直接跳到3月。甚至在直接传递值之后它会进入游行所以,伙计们请帮助我有这个问题。
<html>
<head>
<title>Calendar</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<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");
}
$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>
</tr>
<tr>
<td width='50px' align='center'>Sun</td>
<td width='50px' align='center'>Mon</td>
<td width='50px' align='center'>Tue</td>
<td width='50px' align='center'>Wed</td>
<td width='50px' align='center'>Thur</td>
<td width='50px' align='center'>Fri</td>
<td width='50px' align='center'>Sat</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++){
echo "<td> </td>";
}
}
if($counter % 7 == 0){
echo "</tr><tr>";
}
echo "<td align='center'>".$i."</td>";
}
echo "</tr>";
?>
</table>
</body>
</html>
答案 0 :(得分:0)
您的代码在本月的大部分时间都有效,而不是在2月份不存在的日子。请看以下行:
if(isset($_GET['day'])){
$day = $_GET['day'];
} else {
$day = date("j");
}
它将当天设置为当月的当天。今天,那是第29届。
接下来,您将创建一个timstamp:
$currentTimeStamp = strtotime("$year-$month-$day");
这会尝试从2014-02-29创建一个失败的数据。
为什么要将当天解析为参数?看起来它没有在代码中使用。一个快速的解决方案可能是忘记day参数,只需创建本月第一天的时间戳。
$currentTimeStamp = strtotime("$year-$month-01");
答案 1 :(得分:0)
在第$currentTimeStamp = strtotime("$year-$month-$day");
行之前添加此代码:
$maxDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if( $day > $maxDay ) {
$day = $maxDay;
}