Calendar Script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,
 
I just finished writing a basic calendar script... I wanted to pass it out to everyone here... maybe someone could use it... or, even better optimize it.. .and maybe share that with me.
 
<?php
 
function Cal_Month()
 {
  // Get Today's Date
   $todays_date = getdate();
  
  // Determine what month to display
   if(isset($_GET['month']))
    {
     $resolve_time = "1 ".$_GET['month']." ".$_GET['year'];
     $resolve_first_day_of_month = strtotime($resolve_time);
     $resolved_date = getdate($resolve_first_day_of_month);
    }
    else
    { $resolved_date = $todays_date; }
  
  // if this is the current month - say it's okay to mark today on the calendar
   if($todays_date['year'] == $resolved_date['year'] && $todays_date['month'] == $resolved_date['month'])
    {  $mark_today_on_calendar = TRUE; }
 
  // HOW MANY DAYS ARE IN THIS MONTH
   $days_in_current_month = cal_days_in_month(CAL_GREGORIAN, $resolved_date['mon'], $resolved_date['year']);
  
  // STRING TO START THE CALENDAR ON THE FIRST DAY OF THE MONTH
   $show_month = $resolved_date['month'];
   $show_year = $resolved_date['year'];
  
  // Get Info for Current Displayed Month 
   $string_time = "1 ".$show_month." ".$show_year;
   $first_day_of_month = strtotime($string_time);
   $first_day = getdate($first_day_of_month); //**************************
   $the_first_day_of_the_month = getdate($first_day_of_month); // Starts from the 1st
   
   $num_days_week = 7;
   $num_days_in_month = cal_days_in_month(CAL_GREGORIAN, $resolved_date['mon'], $resolved_date['year']);
  
  // Get info for NEXT month 
   $show_next = strtotime("+1 Month", $first_day_of_month);
   $show_next_month_first_day = getdate($show_next);
   
  // Get info for PREV month 
   $show_prev = strtotime("-1 Month", $first_day_of_month);
   $show_prev_month_first_day = getdate($show_prev);
   
   $next_month = $show_next_month_first_day['month'];
   $next_year = $show_next_month_first_day['year'];
   $prev_month = $show_prev_month_first_day['month'];
   $prev_year = $show_prev_month_first_day['year']; 
   
   $link_next_month = "<a href="" Month </a>";
   $link_prev_month = "<a href="" Month </a>";
  
  // Table Size Variables... will be moved to CSS eventually
   $table_align = "center";
   $table_width = 500;
   $table_height = 50;
   $cell_width = $table_width/7;   
  // Start HTML for Building Calendar
  
    $cal_generated .= "<center>".$link_prev_month." ".$link_next_month."</center>";
    $cal_generated .= "<table width=\"".$table_width."\" border=\"1\" align=\"".$table_align."\">";
    $cal_generated .=  "<tr><td colspan=\"7\"><center>".$the_first_day_of_the_month['month']." ".$the_first_day_of_the_month['year']."</center></td></tr>";
    $cal_generated .= "<tr><td width=\"".$cell_width."\">Sun</td><td width=\"".$cell_width."\">Mon</td>";
    $cal_generated .= "<td width=\"".$cell_width."\">Tue</td><td width=\"".$cell_width."\">Wed</td><td width=\"".$cell_width."\">Thu</td>";
    $cal_generated .= "<td width=\"".$cell_width."\">Fri</td><td width=\"".$cell_width."\">Sat</td></tr>";
    
  // Initial Calendar Buildin Variables
    $first_week_marked = FALSE;
    $month_day_count = 1;
    $week_day_count = 0;
 
  // Start The Calendar Loop
    do {
      if($mark_today_on_calendar == TRUE && $todays_date['mday'] == $month_day_count)
       { 
        $highlight_today_open = "<font color=\"#990000\">";
        $highlight_today_close = "</font>";
       }
      else
       { 
        $highlight_today_open = "<font color=\"#000000\">";
        $highlight_today_close = "</font>";
       }
    
     // If it is the first day of the week open a ROW
      if($week_day_count == 0)
        { 
         $cal_generated .= "<tr>"; 
        }
      
       if ($first_week_marked == FALSE)
        {
         $blank_cell = 0;
          do {
           if($the_first_day_of_the_month['wday'] <> $blank_cell)
            {
             $cal_generated .= "<td height=\"".$table_height."\" valign=\"top\"></td>";
             $blank_cell++;
            }
           else
            { 
             
             
             $cal_generated .= "<td height=\"".$table_height."\" valign=\"top\">".$highlight_today_open ."".$month_day_count."".$highlight_today_close."</td>";
             $first_week_marked = TRUE;
            }
            
          $week_day_count = $the_first_day_of_the_month['wday'];
           
         } while ($first_week_marked == FALSE);
          
        }
       else
        {
         $cal_generated .= "<td height=\"".$table_height."\" valign=\"top\">".$highlight_today_open ."".$month_day_count."".$highlight_today_close."</td>";
        }
      
      $month_day_count++;
      
      // If it is the last day of the week close ROW
       if($week_day_count == 6)
        { 
         $cal_generated .= "</tr>"; 
        }
        
      $week_day_count++;
     
      // Countings 
       if($week_day_count == 7)
        {$week_day_count=0;}
        
    } while ($month_day_count <= $num_days_in_month);
 // End Calendar Loop
    
 $cal_generated .= "</table>";   
 return $cal_generated;
   
 }
 
echo Cal_Month();
?>
 
Joe Harman
 
 
Joe Harman
--------------------------------
eInternet Design
Biz Ph: 269.983.7627
--------------------------------
 
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux