Hey Harry,

I haven't ever had the time to get into the smarty templating... I keep
coming across apps that use it though.. I may spend some time checking it
out... 

I've included the script with some updates... Also some CSS stuff and there
is a second page that shows the hour break down of that day

Enjoy!
Joe


04 Jan 2005
-------------------------
-------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<title>Calendar</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
<!--
body {
        font-family: Tahoma, Verdana, Helvetica, Arial, sans-serif;
        font-size: 11px;
        color: #000000;
}
-->
</style>
</head>

<body leftmargin="1" marginwidth="1">
<?php 
// Created By Joe Harman - [EMAIL PROTECTED]
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);
                        $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 PREV month      
                        $show_prev = strtotime("-1 Month",
$first_day_of_month); 
                        $show_prev_month_first_day = getdate($show_prev);
                        
                // Get info for NEXT month      
                        $show_next = strtotime("+1 Month",
$first_day_of_month); 
                        $show_next_month_first_day = getdate($show_next);
                        
                
                        
                        $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=".$next_month."&year=".$next_year."\">Next Month </a>";
                        $link_prev_month = "<a
href=\"?month=".$prev_month."&year=".$prev_year."\">Prev Month </a>";
                
                // Table Size Variables... will be moved to CSS eventually
                        $table_align = "center";
                        $table_width = 550;
                        $table_height = 60;
                        $cell_width = $table_width/7;   
                        $table_bgcolor = "#CCCCCC";     
                        $header_cell_bgcolor = "#FFFFFF";
                        $weekday_cell_color = "#FFFFFF";
                        $date_cell_color = "#FFFFFF";
                        $blank_cell_color = "#FEFEFE";
                        $hover_cell_color = "#F4DFDF";
                        
                        $Hover_Cell =
"onMouseOver=\"this.style.backgroundColor='".$hover_cell_color."'\"
onMouseOut=\"this.style.backgroundColor='".$date_cell_color."'\"";
                        $Hover_Cell_On =
"onMouseOver=\"this.style.backgroundColor='#FF0000'\"
onMouseOut=\"this.style.backgroundColor='#FFFF33'\"";
                        $Hover_Cell_Today =
"onMouseOver=\"this.style.backgroundColor='#FFCC66'\"
onMouseOut=\"this.style.backgroundColor='#FFFFFF'\"";

                        
                // Start HTML for Building Calendar
                
                                $cal_generated .=
"<center>".$link_prev_month."&nbsp;&nbsp; -
&nbsp;&nbsp;".$link_next_month."</center><br>";
                                $cal_generated .= "<table
width=\"".$table_width."\" border=\"0\" align=\"".$table_align."\"
cellpadding=\"1\" cellspacing=\"1\" bgcolor=\"".$table_bgcolor."\">";
                                $cal_generated .=  "<tr><td colspan=\"7\"
bgcolor=\"".$header_cell_bgcolor."\"><center><strong>".$the_first_day_of_the
_month['month']."
".$the_first_day_of_the_month['year']."</strong></center></td></tr>";
                                $cal_generated .= "<tr><td
width=\"".$cell_width."\" bgcolor=\"".$weekday_cell_color."\">Sun</td><td
width=\"".$cell_width."\" bgcolor=\"".$weekday_cell_color."\">Mon</td>";
                                $cal_generated .= "<td
width=\"".$cell_width."\" bgcolor=\"".$weekday_cell_color."\">Tue</td><td
width=\"".$cell_width."\" bgcolor=\"".$weekday_cell_color."\">Wed</td><td
width=\"".$cell_width."\" bgcolor=\"".$weekday_cell_color."\">Thu</td>";
                                $cal_generated .= "<td
width=\"".$cell_width."\" bgcolor=\"".$weekday_cell_color."\">Fri</td><td
width=\"".$cell_width."\"
bgcolor=\"".$weekday_cell_color."\">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
        
{       
        
$link_month =  $show_month;
        
$link_year = $show_year;
        
$link_day = $month_day_count;
        
$Day_Link =
"onclick=\"window.location.href='?month=".$link_month."&year=".$link_year."&
day=".$month_day_count."'\"";
        
$cal_generated .= "<td height=\"".$table_height."\" valign=\"top\"
bgcolor=\"".$date_cell_color."\" ".$Hover_Cell."
".$Day_Link.">".$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
                                                                {
        
$link_month =  $show_month;
        
$link_year = $show_year;
        
$link_day = $month_day_count;
        
$Day_Link =
"onclick=\"window.location.href='?month=".$link_month."&year=".$link_year."&
day=".$month_day_count."'\"";
        
$cal_generated .= "<td height=\"".$table_height."\" valign=\"top\"
bgcolor=\"".$date_cell_color."\" ".$Hover_Cell."
".$Day_Link.">".$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;
                        
        }
        
function Cal_Day()
        {       
                // Get Today's Date
                        $todays_date = getdate();
                
                        $resolve_time = $_GET['day']." ".$_GET['month']."
".$_GET['year']." 0:00";
                        $resolve_first_day_of_month =
strtotime($resolve_time);
                        $resolved_date =
getdate($resolve_first_day_of_month);
                        
                // 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_day = $resolved_date['mday'];
                        $show_month = $resolved_date['month'];
                        $show_year = $resolved_date['year'];
                
                // Get Info for Current Displayed Month 
                        $string_time = $_GET['day']." ".$show_month."
".$show_year." 0:00";
                        $display_date = strtotime($string_time);
                        $displayed_day_of_the_month =
getdate($display_date); // Starts from the 1st
                        
                        //print_r($displayed_day_of_the_month);
                        $num_days_to_show = 3;
                        $num_days_in_month =
cal_days_in_month(CAL_GREGORIAN, $resolved_date['mon'],
$resolved_date['year']);
                
                // Get info for PREV month ARRAY
                        $show_prev = strtotime("-1 day", $display_date); 
                        $show_prev_day = getdate($show_prev);
                        //echo $show_prev;
                        //echo "<br>";
                        //print_r($show_prev_day);
                        //echo "<br>";
                
                // Current day ARRAY
                        $show_current = $display_date;
                        $show_current_day = getdate($display_date);
                        //echo $display_date; echo "<br>";
                        //print_r($show_current_day);
                        //echo "<br>";
                
                // Get info for NEXT month ARRAY
                        $show_next = strtotime("+1 day", $display_date); 
                        $show_next_day = getdate($show_next);
                        //echo $show_next; echo "<br>";
                        //print_r($show_next_day);
                        //echo "<br>";

                        $next_day = $show_next_day['mday'];
                        $next_month = $show_next_day['month'];
                        $next_year = $show_next_day['year'];
                        
                        $prev_day = $show_prev_day['mday'];
                        $prev_month = $show_prev_day['month'];
                        $prev_year = $show_prev_day['year'];    
                        
                        $link_next_day = "<a
href=\"?month=".$next_month."&year=".$next_year."&day=".$next_day."\">Forwar
d 1 Day </a>";
                        $link_prev_day = "<a
href=\"?month=".$prev_month."&year=".$prev_year."&day=".$prev_day."\">Backwa
rd 1 Day</a>";
                
                // Table Size Variables... will be moved to CSS eventually
                        $table_align = "center";
                        $table_width = 550;
                        $table_height = 10;
                        $cell_time_width = 40;
                        $cell_width = ($table_width -
$cell_time_width)/$num_days_to_show;    
                        $table_bgcolor = "#CCCCCC";     
                        $header_cell_bgcolor = "#FFFFFF";
                        $weekday_cell_color = "#FFFFFF";
                        $date_cell_color = "#FFFFFF";
                        $blank_cell_color = "#FEFEFE";
                        $hover_cell_color = "#F4DFDF";
                        
                        $Hover_Cell =
"onMouseOver=\"this.style.backgroundColor='".$hover_cell_color."'\"
onMouseOut=\"this.style.backgroundColor='".$date_cell_color."'\"";
                        $Hover_Cell_On =
"onMouseOver=\"this.style.backgroundColor='#FF0000'\"
onMouseOut=\"this.style.backgroundColor='#FFFF33'\"";
                        $Hover_Cell_Today =
"onMouseOver=\"this.style.backgroundColor='#FFCC66'\"
onMouseOut=\"this.style.backgroundColor='#FFFFFF'\"";

                        
                // Start HTML for Building Calendar
                
                                $cal_generated .=
"<center>".$link_prev_day."&nbsp;&nbsp; -
&nbsp;&nbsp;".$link_next_day."</center><br>";
                                $cal_generated .= "<table
width=\"".$table_width."\" border=\"0\" align=\"".$table_align."\"
cellpadding=\"1\" cellspacing=\"1\" bgcolor=\"".$table_bgcolor."\">";
                                $cal_generated .=  "<tr><td colspan=\"4\"
bgcolor=\"".$header_cell_bgcolor."\"><center><strong>".$show_current_day['mo
nth']."
".$the_first_day_of_the_month['year']."</strong></center></td></tr>";
                                $cal_generated .= "<tr>";
                                $cal_generated .= "<td
width=\"".$cell_time_width."\">&nbsp</td>";
                                $cal_generated .= "<td
width=\"".$cell_width."\"
bgcolor=\"".$weekday_cell_color."\">".$show_prev_day['mon']."/".$show_prev_d
ay['mday']."/".$show_prev_day['year']." -
".$show_prev_day['weekday']."</td>";
                                $cal_generated .= "<td
width=\"".$cell_width."\"
bgcolor=\"".$weekday_cell_color."\">".$show_current_day['mon']."/".$show_cur
rent_day['mday']."/".$show_current_day['year']." -
".$show_current_day['weekday']."</td>";
                                $cal_generated .= "<td
width=\"".$cell_width."\"
bgcolor=\"".$weekday_cell_color."\">".$show_next_day['mon']."/".$show_next_d
ay['mday']."/".$show_next_day['year']." -
".$show_next_day['weekday']."</td>";
                                $cal_generated .= "</tr>";
                                
                                
                // Initial Calendar Buildin Variables
                                $first_week_marked = FALSE;
                                $month_day_count = 1;
                                $week_day_count = 0;

                                
                                                
                                                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>";
                                                        }
                                
                                                
        
$link_month =  $show_month;
        
$link_year = $show_year;
        
$link_day = $month_day_count;
        
$Day_Link =
"onclick=\"window.location.href='?month=".$link_month."&year=".$link_year."&
day=".$month_day_count."'\"";
                                                                        
        
$temp_filler = "&nbsp;";
                                                        
                                                        $count_hours = 0;
                                                        $add_time =
$show_current;
                                                        
                                                        $epoc_time_prev =
$show_prev; 
                                                        $epoc_time_current =
$show_current;
                                                        $epoc_time_next =
$show_next;
                                                        
                                                        do {
                                                        
        
$cal_generated .= "<tr>";
                                                                        
                                                                        
        
$military_column = date("H:i", $add_time);
        
$add_time = $add_time + 3600;
                                                                        
                                                                        
                                                                        
                                                                        //
Time Coloumn
        
$cal_generated .= "<td width=\"".$cell_time_width."\"
align=\"right\">".$military_column."&nbsp;</td>";
                                                                        
        
//$show_prev
        
$prev_hour_Link =
"onclick=\"window.location.href='?epoc=".$epoc_time_prev."'\"";
                                                                        //
Previous Day
        
$cal_generated .= "<td height=\"".$table_height."\" valign=\"top\"
bgcolor=\"".$date_cell_color."\" ".$Hover_Cell."
".$prev_hour_Link.">".$temp_filler."</td>";
                                                                        
        
//$show_current_day
        
$current_hour_Link =
"onclick=\"window.location.href='?epoc=".$epoc_time_current."'\"";
                                                                        //
Current Day
        
$cal_generated .= "<td height=\"".$table_height."\" valign=\"top\"
bgcolor=\"".$date_cell_color."\" ".$Hover_Cell."
".$current_hour_Link.">".$temp_filler ."</td>";
                                                                        
        
//$show_next
        
$next_hour_Link =
"onclick=\"window.location.href='?epoc=".$epoc_time_next."'\"";
        
//Next Day
        
$cal_generated .= "<td height=\"".$table_height."\" valign=\"top\"
bgcolor=\"".$date_cell_color."\" ".$Hover_Cell."
".$next_hour_Link.">".$temp_filler ."</td>";
                                        
        
$cal_generated .= "</tr>";
                                                                
        
$epoc_time_prev = $epoc_time_prev + 3600; 
        
$epoc_time_current = $epoc_time_current + 3600;
        
$epoc_time_next = $epoc_time_next + 3600;
        
$count_hours++;
                                                                
                                                        } while
($count_hours <= 23);
                                                        
                                                                
                                                $week_day_count++;
                                        
                                                // Countings    
                                                        if($week_day_count
== 7)
        
{$week_day_count=0;}
                                                                
                                
                $cal_generated .= "</table>";                   
                return $cal_generated;
        }
        
        if(isset($_GET['day']))
                {
                        echo Cal_Day();
                }
        else
                {
                        echo Cal_Month();
                }
?>
</body>
</html>


----------------------------------------------------------------------------
-------






-----Original Message-----
From: HarryG [mailto:[EMAIL PROTECTED] 
Sent: Monday, January 03, 2005 7:37 AM
To: php-general@lists.php.net
Subject: [PHP] Re: Calendar Script

Cool.. just tried your script. I might try to convert it to Smarty template
compatible code, in my spare time. Have you used Smarty Templates??
  "Joe Harman" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
  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=".$next_month."&year=".$next_year."\">Next Month </a>";
     $link_prev_month = "<a
href=\"?month=".$prev_month."&year=".$prev_year."\">Prev 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to