Arafat
Arafat

Reputation: 143

JavaScript Calender is not working

I am trying to make a calender using php according to a video tutorial from youtube. I m doing just what they do but there is a difference from their table.I can't find my mistake...any body help please....give the code and the picture of this....

This is my calender-

calender

This is the tutorial calender-

calender

<html>
<head>
    <title>Calender</title>
</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>
            <th><input  style = "width : 50px"type = "button" value = "<" name = "prevbutton"></input></th>
            <th colspan = "5"><?php echo $monthName.", ".$year?></th>
            <th><input style = "width : 50px" type = "button" value = ">" name = "nextbutton"></input></th>
        </tr>

        <tr>
            <td width = "50px">Sun</td>
            <td width = "50px">Mon</td>
            <td width = "50px">Tue</td>
            <td width = "50px">Wed</td>
            <td width = "50px">Thu</td>
            <td width = "50px">Fri</td>
            <td width = "50px">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 "</tr>";
        ?>

    </table>
</body>

Upvotes: 0

Views: 90

Answers (1)

Darek MST
Darek MST

Reputation: 117

You had 3 bugs in php code

  echo "<tr>";
            for($i = 1;$i<$numDays+1;$i++,$counter++)
            {
                $timeStamp = strtotime($year.'-'.$month.'-'.$i);// it should be string
                if($i == 1)
                {
                    $firstDay = date('w', $timeStamp);
                    for($j = 0;$j<$firstDay;$j++,$counter++)
                    {
                        echo "<td>&nbsp</td>"; // show days
                    }
                  $counter++;  // first row counter correction
                }

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

                if($counter%7==0)
                {
                    echo "</tr><tr>";
                }
            }
        echo "</tr>";

Upvotes: 1

Related Questions