简体   繁体   English

无法在php日历中打印2月

[英]not able to print february month in php calendar

this is the code i had created for generating a calendar using php and html.but this code is not working for February month it directly jumps to march .even after passing the values directly it goes to march so,guys please help me with this problem. 这是我为使用php和html生成日历而创建的代码,但是此代码不适用于2月份,即使直接传递了值,它也会直接前进到进行。所以,请帮我解决这个问题。

<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>&nbsp;</td>";
            }
        }
        if($counter % 7 == 0){
            echo "</tr><tr>";
        }
        echo "<td align='center'>".$i."</td>";

    }

    echo "</tr>";
    ?>
</table>
</body>
</html>

Your code works most of the days of the month, just not on the days that don't exist in February. 您的代码在一个月中的大部分时间都有效,而在2月不存在的日子中则无效。 Look at the following line: 查看以下行:

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

It sets the day to the current day of the month. 它将日期设置为该月的当前日期。 Today, that is the 29th. 今天,是29日。

Next, you create a timstamp: 接下来,创建一个timstamp:

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

This attempts to create a data from 2014-02-29, which will fail. 这会尝试从2014-02-29创建数据,该数据将失败。

Why do you want to parse the day as a parameter? 为什么要将日期解析为参数? It looks like it isn't used in the code. 看起来代码中没有使用它。 A quick solution could be to forget the day parameter, and just create the timestamp from the first of the month. 一种快速的解决方案是忘记day参数,而仅从该月的第一天开始创建时间戳。

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

Add this code before the line $currentTimeStamp = strtotime("$year-$month-$day"); $currentTimeStamp = strtotime("$year-$month-$day");行之前添加此代码$currentTimeStamp = strtotime("$year-$month-$day"); :

$maxDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if( $day > $maxDay ) {
    $day = $maxDay;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM