Help with this PHP Calendar

Discussion in 'PHP' started by saadi123, Feb 22, 2014.

  1. #1
    I am working on PHP calendar code and now I'm stuck at a place. What I want is that the code works in the following way.

    1. User sets the date range in the date_setter.php page.
    2. The data is then transferred to the calendar.php page using GET method.
    3. The calendar.php page displays the pre-specified months where each month is displayed separately.

    Following is the code:
    date_setter.php
    
    <!DOCTYPE HTML>
        <html lang = "eng">
            <head>
                <title>Date Setter</title>
            </head>
            <body>
                    <form method = 'GET' action = 'test_calendar.php' name='date_setter'>
                        <select name="from_calendar_month" id="calendar_month">
                            <option value="from_blank_month"></option>
                                <?php
                                    for ($i=1;$i<=12;$i++)
                                    {
                                        $month = date("F",mktime(0,0,0,$i,1,2014)); ?>
                                        <option value="<?php echo $month; ?>"><?php echo $month .'</option>';
                                    }
                                ?>
                        </select>
                       
                        <select name="from_calendar_year" id="from_calendar_year">
                            <option value="2001" >2001</option>
                            <option value="<?php echo date("Y");?>" selected>2014</option>
                            <option value="2015" >2015</option>
                            <option value="2013" >2013</option>
                        </select>
                       
                        <br>
                       
                        <select name="to_calendar_month" id="to_calendar_month">
                            <option value="to_blank_month"></option>
                                <?php
                                    for ($i=1;$i<=12;$i++)
                                    {
                                        $month = date("F",mktime(0,0,0,$i,1,2014)); ?>
                                        <option value="<?php echo $month; ?>"><?php echo $month .'</option>';
                                    }
                                ?>
                        </select>
                       
                        <select name="to_calendar_year" id="to_calendar_year">
                            <option value="2001" >2001</option>
                            <option value="<?php echo date("Y");?>" selected>2014</option>
                            <option value="2015" >2015</option>
                            <option value="2013" >2013</option>
                        </select>
                       
                        <br>
                       
                        <input type="submit" name="calendar_submit">
                           
                    </form>
            </body>
        </html>
    
    
    Code (markup):
    Now the code from
    test_calendar.php
    
    <!DOCTYPE HTML>
        <html lang="eng">
            <head>
                <title>PHP Calendar</title>
                <link rel="stylesheet" type="text/css" href="../calendar_css.css">
            </head>
            <body>
                <?php
                   
                    /* Draws a calendar*/
                    function draw_calendar($month, $year)
                        {
                            /* Draw table*/
                            $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
                            $month = date('m',strtotime($_GET['from_calendar_month'])); //Numeric representation of the month.
                            $year = $_GET['from_calendar_year'];
                           
                            $min_month = $_GET['from_calendar_month'];
                            $max_month = $_GET['to_calendar_month'];
                            $min_year = $_GET['from_calendar_year'];
                            $max_year = $_GET['to_calendar_year'];
                            $min_month_num = date('n',strtotime($min_month));
                            $max_month_num = date('n',strtotime($max_month));
                            $monthCounter = ($max_year-$min_year)+($max_month_num-$min_month_num)+1;
                           
                           
                            /*Table heading*/
                            $headings = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
                            $calendar .= '<tr><th border="solid black">' . $year . '</th></tr><tr><th>' . $month . '</th></tr>';
                            $calendar .= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
                           
                           
                            /*row for week one*/
                            $calendar .= '<tr class="calendar-row">'; 
                            $running_day = date('w',mktime(0,0,0,$month,1,$year)); //Numeric representation of the day. 0 for sunday e.t.c...
                            $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
                            $days_in_this_week = 1;
                            $day_counter = 0;
                            $dates_array = array();
                           
                            /*Print blank days until first of the current week*/
                            for ($x=0;$x<$running_day;$x++)
                            {
                                $calendar .= '<td class = "calendar-day-np"> </td>';
                                $days_in_this_week++;
                            }
                           
                            /*Keep going with days...*/
                            for($list_day=1;$list_day<=$days_in_month;$list_day++)
                            {
                                $calendar .= '<td class = "calendar-day">';
                               
                                /*Add in the day number*/
                                $calendar .= '<div class = "day-number">' . $list_day. '</div>';
                               
                                /*Query database for an entry on this day. If found, print them on them*/
                                $calendar .= str_repeat('<p> </p>', 2);
                               
                                $calendar .= '</td>';
                                if ($running_day == 6)
                                {
                                    $calendar .= '</tr>';
                                    if (($day_counter+1) != $days_in_month)
                                    {
                                      $calendar .= '<tr class ="calendar-row">';
                                    }
                                    $running_day = -1;
                                    $days_in_this_week = 0;
                                }
                               
                                $days_in_this_week++; $running_day++; $day_counter++;
                            }
                           
                            /*Finish the rest of the days in the week*/
                            if($days_in_this_week < 8)
                            {
                                for ($x=1;$x<=(8-$days_in_this_week); $x++)
                                {
                                    $calendar .='<td class ="calendar-day-np"> </td>';
                                }
                            }
                           
                            /*Final row*/
                            $calendar .= '</tr>';
                           
                            /*End of table*/
                            $calendar .= '</table>';
                           
                            for($x=($min_month_num-1); $x<=$monthCounter;$x++)
                            {
                            $month = $x;
                            $year = $min_year;
                           
                                if($x==12)
                                {
                                    $x == 0;
                                    $year == $year+1;
                                }
                            }
                           
                           
                            /*all done, return result*/
                            return $calendar;
                            return $month."<br>";
                            return $year."<br>";
                       
                           
                        }
                        echo draw_calendar($month, $year); 
                    ?>
           </body>
    </html>
    
    Code (markup):
    Thank you.
     
    saadi123, Feb 22, 2014 IP