Saturday 16 July 2011

How To Create Calander in PHP

<?php
//ini_set('date.timezone','UTC');

/* ***  Event Calander *******
        Developed By Webmaster
        Server should have date.timezone equals to UTC at php.ini
        else calander will not generate
*/
        //URL is like   http://mysite/test/eventcalander.php?stat=1
        /*
            $_GET['stat']; status coming vai POST or get method from URL
            0-Generate previous month ,1- Generate current month, 2- Generate next month
        */
$stat = $_GET['stat'];
$nmonth = $_GET['nmonth'];
$nyear = $_GET['nyear'];
        // *** $nmonth,$nyear will show numeric value of month,year whose Calander is showing currently at page
if($stat=="")
    $stat=1; //by default current month as status
if($nmonth=="")
    $nmonth = date('n'); //by default current month's numeric value
if($nyear=="")
    $nyear = date('Y'); //by default current year's numeric value

$calander = generateCalander($stat,$nmonth,$nyear);

?>


<form method="GET" name="frmcal" id="frmcal">
<center>
<div style="padding:20px 10px 12px 10px;">
    <?php echo $calander;?>
</div>
<input type="hidden" id="stat" name="stat" value="<?php echo $stat;?>">
<input type="hidden" id="nmonth" name="nmonth">
<input type="hidden" id="nyear" name="nyear">
<!-- nmonth stores month in numeric value whose calander is showing presently showing at page
    or we can use SESSION variable if using Ajax to store the numeric value of month such
    as 1-Jan 2 for Feb or 7 for july...this is important to keep track of which month
    we are generating calander for and which we are showing right now...
-->
</form>


<?php
function generateCalander($q,$nmonth,$nyear){   
       
/* ********* Simple Event Calander in PHP ***** */
    $stat=$q;
    $str="";       
    if($stat=='1'){
            //generate calander for current month
        $today=date('d'); 
            //This gets today's date
        $date =time() ; // this show o/p 1310737041 unix timestamp for today
        $tempmonth = $nmonth;
        $tempyear = $nyear;
    }   
    else{
        //generate calander for other month
        $tempcmon = $nmonth; // month showing at present in calander in numeric value   
        $tempcyr = $nyear; // year showing at present in calander in numeric value;
        if($stat=="2"){
            //Generate Calander for Next Month
                if($tempcmon==12){
                    $tempmonth = 1; //current month is dec so next is Jan
                    $tempyear = $tempcyr + 1;
                } else{
                    $tempmonth = $tempcmon + 1;
                    $tempyear = $tempcyr;
                }
                $OtherMonthTime = mktime(0, 0, 0,$tempmonth,1, $tempyear);
       
                /* *** mktime() is same as time() and return Unix Time but time does not
                need parameters
                mktime($hr,$min,$sec,$mon,$day,$year) all numeric else E_Strict error occurs
                */
        } else{
                // Generate Calander for Previous Month
                if($tempcmon==1){
                    $tempmonth = 12; //current month is Jan so Prev is Dec
                    $tempyear = $tempcyr - 1;
                } else{
                    $tempmonth = $tempcmon - 1;
                    $tempyear = $tempcyr;
                }
                $OtherMonthTime = mktime(0, 0, 0, $tempmonth,1, $tempyear);
                /* ** $OtherMonthTime contain Unix Time like 32558941 for our required month for 1 date
                */
        }
        $date = $OtherMonthTime;
        $today=date('d');
    }   
   
    //This puts the day, month, and year in seperate variables
    $day = date('d', $date) ; //will show Day for ex say 15
    $month = date('m', $date) ; //will show month say July
    $year = date('Y', $date) ; //will show year say 2011
        //Here we generate the first day of the month
    $first_day = mktime(0,0,0,$month, 1, $year) ;        
    $title = date('F', $first_day) ; //This gets us the month name
        //Here we find out what day of the week the first day of the month falls on
    $day_of_week = date('D', $first_day) ;
         /* 
            Once we know what day of the week it falls on, we know how many blank days occure before it.
            If the first day of the week is a Sunday then it would be zero
         */
    switch($day_of_week){
         case "Sun":
            $blank = 0;
            break;
         case "Mon":
            $blank = 1;
            break;
         case "Tue":
            $blank = 2;
            break;
         case "Wed":
            $blank = 3;
            break;
         case "Thu":
            $blank = 4;
            break;
         case "Fri":
            $blank = 5;
            break;
         case "Sat":
            $blank = 6;
            break;
    }
     /*
        Here we take a closer look at the days of the month and prepare to make our calendar table.
        The first thing we do is determine what day of the week the first of the month falls.
        Once we know that, we use the switch () function to determine how many blank days we need
        in our calendar before the first day.
        Next we count the total days of the month. Now that we know how many 'blank' days we need,
        and how many total days are in the month we can start to generate our calendar.
     */
            //We then determine how many days are in the current month
    $days_in_month = cal_days_in_month(0, $month, $year);
            //Here we start building the table heads
     $prevst="0";
     $nextst="2";
     $str= '<table border="1" cellspacing="1" cellpadding="4"><tbody>';
     $str.= '<tr><td colspan="5" bgcolor="#e0d6cb" class="black_12"> '.$title.' '.$year.' </td>';
     $str.='<td bgcolor="#e0d6cb"><a href="javascript:void(0);" onclick="callcalander('.$prevst.','.$tempmonth.','.$tempyear.');">   
     <div>--</div></a></td>
     <td bgcolor="#e0d6cb"><a href="javascript:void(0);" onclick="callcalander('.$nextst.','.$tempmonth.','.$tempyear.');">
     <div>--</div></a>
     </td></tr>';
     $str.= '<tr class="black_12">
     <td width="13%" bgcolor="#e0d6cb">Sun</td>
     <td width="13%" bgcolor="#e0d6cb">Mon</td>
     <td width="13%" bgcolor="#e0d6cb">Tue</td>
     <td width="13%" bgcolor="#e0d6cb">Wed</td>
     <td width="13%" bgcolor="#e0d6cb">Thu</td>
     <td width="13%" bgcolor="#e0d6cb">Fri</td>
     <td width="13%" bgcolor="#e0d6cb">Sat</td></tr>';
            //This counts the days in the week, up to 7
     $day_count = 1;
     $str.= '<tr class="week">';
            //first we take care of those blank days
     while($blank > 0)
     {
        $str.= '<td class="blank black_12" height="25" align="center"></td>';
        $blank = $blank-1;
        $day_count++;
     }
    /*
    The first part of this code very simply echos the table tags, the month name, and the headings for the days of
    the week. Then we start a while loop. What we are doing is echoing empty table details, one for each
    blank day we count down. Once the blank days are done it stops. At the same time, our $day_count
    is going up by 1 each time through the loop. This is to keep count so that we do not try to put
    more than seven days in a week.
    */

            //sets the first day of the month to 1
    $day_num = 1;
            //count up the days, untill we've done all of them in the month
    while($day_num <= $days_in_month)
    {
         if($day_num==$today)
             $class="today black_12";
         else
             $class="black_12";

         $str.= '<td class="'.$class.'" height="25" align="center">'.$day_num.'</td>';
         $day_num++;
         $day_count++;
            //Make sure we start a new row every week
        if($day_count > 7)
        {
            $str.='</tr><tr>';
            $day_count = 1;
        }
    }
    /*
        Now we need to fill in the days of the month. We do this with another while loop,
        but this time we are counting up to the last day of the month. Each cycle echos a table detail
        with the day of the month, and it repeats until we reach the last day of the month.
        Our loop also contains a conditional statement. This checks if the days of the week have reached 7,
        the end of the week. If it has, it starts a new row,
        and resets the counter back to 1 (the first day of the week).
    */
            //Finaly we finish out the table with some blank details if needed
     while($day_count >1 && $day_count <=7)
     {
        $str.='<td></td>';
        $day_count++;
     }
    $str.='</tr></tbody></table>';
/* Calander Code Ends ****/

    return $str;
   
}
?>
<script>
function callcalander(val1,val2,val3){
    document.getElementById('stat').value=val1;
    document.getElementById('nmonth').value=val2;
    document.getElementById('nyear').value=val3;
    document.frmcal.submit();
}
</script>


download php file from following ink
http://www.keepandshare.com/doc/2989630/eventcalander-zip-july-16-2011-2-57-pm-3k?da=y

No comments:

Post a Comment