0

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.

<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>
Luke
  • 22,826
  • 31
  • 110
  • 193

2 Answers2

0

Your code works most of the days of the month, just not on the days that don't exist in February. 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.

Next, you create a timstamp:

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

This attempts to create a data from 2014-02-29, which will fail.

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.

$currentTimeStamp = strtotime("$year-$month-01");
Seshoumaro
  • 86
  • 2
  • it's working.....but i have another query- when ever the month starts on sunday it leaves a blank row????....please reply – Prashanth B Sep 29 '14 at 10:34
  • It works fine for me; for example february 2015 is printed correctly. Sunday the 1st is printed without an empty line. – Seshoumaro Sep 29 '14 at 10:36
  • it's working fine without css....may be some bug in css....thank you for your answer it works – Prashanth B Sep 29 '14 at 10:45
0

Add this code before the line $currentTimeStamp = strtotime("$year-$month-$day");:

$maxDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if( $day > $maxDay ) {
    $day = $maxDay;
}
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15