Free Form Calendar Application

Ok as I was searching around one day for calendar scripts out there I found a nice little blog by an author David Walsh (his name is included in the credits) which had a really nice, short, sweet and simple but very effective calendar setup. I took it and modified in my own likings but also transformed the script (from a function) to a working class that you can add to any application (free standing PHP or a framework) and still have the same functionality.

Demo: http://www.sterlingsavvy.com/tutorials/calendar/

The features:

The class has it’s own CSS design. But you can define multiple design sheets (since you can call multiple calendars for one site).

This effectively allows you to have two or more calendars on your site with different CSS properties. Or you could have multiple designs for your site and then have a matching calendar design for each template - that way when you call the instance of the class you just specify the design based on what the user’s template setting is.

Because it is a class you can define multiple instances of the calendar for your site. This could be effective for putting a small mini calendar on your main page and then having a bigger calendar application on your actual calendar page.

Finally it includes a full control menu with previous and next month links as well as select month and year.

The form select option performs via post request. The next and previous work on a link basis and thus apply a get request. If the post is not set then the values are swapped over to whats stored in the get request. If the get request is null, the application shows a default of the current month and year. :)

It’s hefty code but very efficient and worth it’s weight.

All you have to do for the Yii framework is define this as a component and you can use it within your framework controllers and views.

The documentation is very thorough and there are hints of what you can change where for what purposes. There is also indication of where you would insert your events (pulled from a database). That code is not supplied, just sample styling of an event. You guys can perform the database work yourself ;)

[Edit] Because it was bogging the system to load so much data in one post, I broke it up in several posts, they follow in suite.

The Calendar Class




<?php

/*

  * This Calendar Application was designed by:

  *

  *         Kyle Ferreira

  *         http://www.SterlingSavvy.com

  *

  * Credits for following the template of:

  *

  *         David Walsh

  *        http://davidwalsh.name/php-calendar

  *

  * You are free and welcome to use and manipulate this code as you wish.

  * Please keep a reference to those who helped shape it (ie: the sources above)

  */


/*

  * This is a Calendar Application Class

  *

  * Because this is a calendar application I've included a reference of all the Date()

  * format text to call so you can customize your own look and feel <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

  *

  * a    =    'am' or 'pm'

  * A    =    'AM' or 'PM'

  * B    =    Swatch Internet time

  * d    =    day of the month, 2 digits with leading zeros; i.e. '01' to '31'

  * D    =    day of the week, textual, 3 letters; i.e. 'Fri'

  * F    =    month, textual, long; i.e. 'January'

  * g    =    hour, 12-hour format without leading zeros; i.e. '1' to '12'

  * G    =    hour, 24-hour format without leading zeros; i.e. '0' to '23'

  * h    =    hour, 12-hour format; i.e. '01' to '12'

  * H    =    hour, 24-hour format; i.e. '00' to '23'

  * i    =    minutes; i.e. '00' to '59'

  * I    =    '1' if Daylight Savings Time, '0' otherwise.    --> Note this is Capital i --> I

  * j    =    day of the month without leading zeros; i.e. '1' to '31'

  * l    =    day of the week, textual, long; i.e. 'Friday'    --> Note this is Lowercase L --> l

  * L    =    boolean for whether it is a leap year; i.e. '0' or '1'

  * m    =    month; i.e. '01' to '12'

  * M    =    month, textual, 3 letters; i.e. 'Jan'

  * n    =    month without leading zeros; i.e. '1' to '12'

  * r    =     RFC 822 formatted date; i.e. 'Thu, 21 Dec 2000 16:01:07 +0200' (added in PHP 4.0.4)

  * s    =     seconds; i.e. '00' to '59'

  * S    =     English ordinal suffix, textual, 2 characters; i.e. 'th', 'nd'

  * t    =     number of days in the given month; i.e. '28' to '31'

  * T    =     Timezone setting of this machine; i.e. 'MDT'

  * U    =     seconds since the epoch

  * w    =     day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)

  * Y    =     year, 4 digits; i.e. '1999'

  * y    =     year, 2 digits; i.e. '99'

  * z    =     day of the year; i.e. '0' to '365'

  * Z    =     timezone offset in seconds (i.e. '-43200' to '43200').

  * The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.

  */

class Calendar

{

     /*

      * Variables for the class

      *

      * Title: refers to the month that should be placed in the heading of the view

      * Calendar: holds the completed calendar

      * Month: holds month

      * Year: holds year value

      * Style: refers to which css style to apply to the calendar (default is: calendar classes)

      * Select Month: Used in the control menu to select which month to view

      * Select Year: Used in the control menu to select which year to view

      * Next Month: Used to select the next month in the control menu

      * Previous Month: Used to select the previous month in the control menu

      * Year Next Month: Stores the year information for the next month control

      * Year Previous Month: Stores the year information for the previous month control

      * Next Month Url: Sets the data for the link

      * Previous Month Url: Sets the data for the link

      * Store Next Link: Sets the link for the view

      * Store Previous Link: Sets the link for the view

      * Display Controls: Shows all the control options in the menu

      * Start Form: holds the <form> tag to start the form as well as table information

      * Close Form: holds the </form> tag to close the form as well as table information

      */

     public $title;

     public $calendar;

     public $month;

     public $year;

     public $style;

     public $selectMonth;

     public $selectYear;

     public $nextMonth;

     public $previousMonth;

     public $yearNextMonth;

     public $yearPreviousMonth;

     public $nextMonthUrl;

     public $previousMonthUrl;

     public $storeNextLink;

     public $storePreviousLink;

     public $displayControls;

     public $startForm;

     public $closeForm;

    

     /*

      * Constructor

      *

      * By Default set the month year to null so the applications will grab the

      * current month and year in the event the none is specified

      */

     function __construct($month = NULL, $year = NULL, $style = "calendar")

     {

         $this->month = $month;

         $this->year = $year;

         $this->style = $style;

         $this->init();

     }

    

     /*

      * Initializing function that runs upon class instantiation

      *

      * Sets the title of the application

      * Then draws the calendar but does not print it

      * Printing has to be be called via printCalendar

      */

     public function init()

     {

         /*

          * Call set title function

          * Call draw calendar function

          * Default gets the current month year for the title.

          * Overriding month and year will alter this for both

          * functions.

          */

         $this->setTitle();

         $this->setControlMenu();

         $this->drawCalendar();

     }

    

     /*

      * Set the title

      * If there is another function that passed the date, either by calendar manipulation

      * (like moving to the next month or previous year etc.) or if it was manually overrided

      * by the user / developer - it'll set the proper header title accordingly.

      */

     public function setTitle()

     {

         if(($this->month == NULL) || ($this->year == NULL))

         {

             $this->title = date("F Y");    

         }

         else

         {

             $this->title = date('F Y',mktime(0,0,0,$this->month,1,$this->year));

         }        

     }

    

     /*

      * Print out the control menu

      */

     public function printControlMenu()

     {

         return $this->displayControls;

     }

    

     /*

      * Print out starting tag of form

      */

     public function printStartForm()

     {

         return $this->startForm;

     }

    

     /*

      * Print out closing tag of form

      */

     public function printCloseForm()

     {

         return $this->closeForm;

     }

    

     /*

      * Builds the control menu

      */

     public function setControlMenu()

     {

         /*

          * If the application didn't provide $month / $year data

          * Set the default values to the current month and year

          * We only set this once here and it'll be used and implied

          * for the drawCalendar function. Since this function is called

          * first through the init() function

          */

         if(($this->month == NULL) || ($this->year == NULL))

         {

             // Month in numbers with the leading 0

             $this->month = date("m");    

             $this->year = date("Y");    

         }

        

         /* select month control */

         $this->selectMonth = '<select class="'. $this->style .'-control-select" name="month" id="month">';

         for($x = 1; $x <= 12; $x++)

         {

             $this->selectMonth.= '<option value="'.$x.'"'.

             ($x != $this->month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$this->year)).'</option>';

         }

         $this->selectMonth.= '</select>';

        

         /* select year control */

         // Alter this year_range number to change how many years difference are selectable.

         $year_range = 7;

         $this->selectYear = '<select class="'. $this->style .'-control-select" name="year" id="year">';

         for($x = ($this->year-floor($year_range/2)); $x <= ($this->year+floor($year_range/2)); $x++)

         {

             $this->selectYear.= '<option value="'.$x.'"'.($x != $this->year ? '' : ' selected="selected"').'>'.$x.'</option>';

         }

         $this->selectYear.= '</select>';

        

         /* "next month" control and "previous month" control */

         /* For plain old PHP

         $this->nextMonth = '<a href="?month='.($this->month != 12 ? $this->month + 1 : 1).

         '&year='.($this->month != 12 ? $this->year : $this->year + 1).'" class="control">Next Month >></a>';

         $this->previousMonth = '<a href="?month='.($this->month != 1 ? $this->month - 1 : 12).

         '&year='.($this->month != 1 ? $this->year : $this->year - 1).'" class="control"><<     Previous Month</a>'; */

        

         // For Yii Framework

         $this->nextMonth = ($this->month != 12 ? $this->month + 1 : 1);

         $this->yearNextMonth = ($this->month != 12 ? $this->year : $this->year + 1);

         $this->previousMonth = ($this->month != 1 ? $this->month - 1 : 12);

         $this->yearPreviousMonth = ($this->month != 1 ? $this->year : $this->year - 1);    

        

         /* Bring all the controls together in a menu */

         $this->startForm = '<table cellpadding="0" cellspacing="0" class="'. $this->style .'-control"><tr><td><form method="post">';

         $this->displayControls = $this->selectMonth.$this->selectYear.

             ' <input type="submit" name="submit" value="Go" />';

         $this->closeForm = '</form></td></tr></table>';

     }

    

     /*

      * Print out the Calendar to the screen

      */

     public function printCalendar()

     {

         return $this->calendar;

     }

    

     /*

      * Draw the Calendar

      */

     public function drawCalendar()

     {

         /* We need to take the month value and turn it into one without a leading 0 */

         if((substr($this->month, 0, 1)) == 0)

         {

             // if value is between 01 - 09, drop the 0

             $tempMonth = substr($this->month, 1);												

             $this->month = $tempMonth;

         }

        

         /* draw table */

         $this->calendar = '<table cellpadding="0" cellspacing="0" class="'. $this->style .'">';

        

         /* table headings */

         $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

         $this->calendar.= '<tr class="'. $this->style .'-row"><td class="'. $this->style .'-day-head">'

             .implode('</td><td class="'. $this->style .'-day-head">',$headings).'</td></tr>';


         /* days and weeks vars now ... */

         $running_day = date('w',mktime(0,0,0,$this->month,1,$this->year));

         $days_in_month = date('t',mktime(0,0,0,$this->month,1,$this->year));

         $days_in_this_week = 1;

         $day_counter = 0;

         $dates_array = array();

    

         /* row for week one */

         $this->calendar.= '<tr class="'. $style .'-row">';

    

         /* print "blank" days until the first of the current week */

         for($x = 0; $x < $running_day; $x++):

             $this->calendar.= '<td class="'. $this->style .'-day-np"> </td>';

             $days_in_this_week++;

         endfor;

    

         /* keep going with days.... */

         for($list_day = 1; $list_day <= $days_in_month; $list_day++):

             if($list_day == date("j",mktime(0,0,0,$this->month)))

             {    

                 $this->calendar.= '<td class="'. $this->style .'-current-day">';

             }

             else            

             {    

                 if(($running_day == "0") || ($running_day == "6"))

                 {

                     $this->calendar.= '<td class="'. $this->style .'-weekend-day">';

                 }

                 else

                 {

                     $this->calendar.= '<td class="'. $this->style .'-day">';    

                 }

             }

            

                 /* add in the day number */

                 $this->calendar.= '<div class="'. $this->style .'-day-number">'.$list_day.'</div>';

    

                 /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/

                 $this->calendar.= '<div class="'. $this->style .'-text"><a href="">Event 1</a></div><br/>';

                 $this->calendar.= '<div class="'. $this->style .'-text"><a href="">Event 2</a></div>';

                 $this->calendar.= str_repeat('<p> </p>',2);

                

             $this->calendar.= '</td>';

             if($running_day == 6):

                 $this->calendar.= '</tr>';

                 if(($day_counter+1) != $days_in_month):

                     $this->calendar.= '<tr class="'. $this->style .'-row">';

                 endif;

                 $running_day = -1;

                 $days_in_this_week = 0;

             endif;

             $days_in_this_week++; $running_day++; $day_counter++;

         endfor;

    

         /* finish the rest of the days in the week */

         if($days_in_this_week < <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> :

             for($x = 1; $x <= (8 - $days_in_this_week); $x++):

                 $this->calendar.= '<td class="'. $this->style .'-day-np"> </td>';

             endfor;

         endif;

    

         /* final row */

         $this->calendar.= '</tr>';

    

         /* end the table */

         $this->calendar.= '</table>';

     }

}

?>



The Calendar CSS




  /*

  * This Calendar Application was designed by:

  *

  *         Kyle Ferreira

  *         http://www.SterlingSavvy.com

  *

  * Credits for following the template of:

  *

  *         David Walsh

  *        http://davidwalsh.name/php-calendar

  *

  * You are free and welcome to use and manipulate this code as you wish.

  * Please keep a reference to those who helped shape it (ie: the sources above)

  */

  

/*     If you wish to make a second calendar class just duplicate this css and change the calendar key words

     to that of the new calendar class you want to use. Like maybe calendar2.

     then when you call the instance of the class of the calendar make sure to specify the style in the

     parameters of the constructor

*/

/* calendar */

/* by not setting a table width you get a table that will cover the size of the div or page you put it in  */

table.calendar {

     border-left:1px solid #999;

}

/* Settings for the control menu */

table.calendar-control {

     width: 100%;

     text-align: center;

}

/* This changes the select month and select year drop down menus */

table.calendar-control-select {  

}


tr.calendar-row {  

}

td.calendar-day, td.calendar-weekend-day, td.calendar-current-day {

     height:100px;

     font-size:11px;

     position:relative;

     vertical-align: top;

     line-height: 4px; /* This determins the space that will  space out the events on the calendar */

}

* html div.calendar-day, div.calendar-weekend-day, div.calendar-current-day {

     height:100px;

}

/* This alters the headings (the days of the week) Change the colour and background for those here */

td.calendar-day-head {

     background:#CCC;

     color: #000;

     font-weight:bold;

     text-align:center;

     width:120px;

     padding:5px;

     border-bottom:1px solid #999;

     border-top:1px solid #999;

     border-right:1px solid #999;

}

/* Change these colours to alter the respective portion of the calendar */

td.calendar-day    {

     background-color: #E1E1E1;

}

td.calendar-weekend-day    {

     background-color: #CFCFCF;

}

td.calendar-current-day    {

     background-color: #ECEFF5;

}

/* These colours are for the mouse over effects on the calendar */

td.calendar-day:hover {

     background:#ECEFF5;

}

td.calendar-weekend-day:hover {

     background:#D7DADF;

}

td.calendar-current-day:hover {

     background:#E5E7ED;

}

/* background colour of the blank dates */

td.calendar-day-np {

     background:#EEE;

     min-height:100px;

}

* html div.calendar-day-np {

     height:100px; }

/* This alters the number dates of the calendar */

div.calendar-day-number {

     position: relative;

     padding:5px;

     color:#000;

     /*

     Uncomment this to make a background square around the dates

     Personally I think it looks ugly, but depending on your colour scheme

     it can work <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

     background-color: #C1C1C1;

     */

     font-weight:bold;

     float:right;

     margin:-5px -5px 0 0;

     width:20px;

     text-align:right;

     line-height: normal;

}

/* This alters the text section for events on the calendar. */

div.calendar-text {

     color:#111;    

}

div.calendar-text a {

     color: #111;

     text-decoration: none;

}

div.calendar-text a:hover {

     color: #333;

     text-decoration: underline;

}

/* shared */

td.calendar-day,td.calendar-current-day, td.calendar-weekend-day, td.calendar-day-np {

     width:120px;

     padding:5px;

     border-bottom:1px solid #999;

     border-right:1px solid #999;

}

  

For Yii Framework

The following line is where you would define the parameter of a different style (pulled from the code of the controller below)




// If the get requests don't have anything, NULL values will be assigned

  $calendar=new Calendar($month,$year,$style);



The application within a controller (I used SiteController and defined a new actionCalendar)




       /**

      * Displays the Calendar

      */

     public function actionCalendar()

     {

         /*

          * If the user selected the month and year via drop down then it's set

          * via post - otherwise they clicked a link, which means it's set by get.

          * If the next / previous month links were clicked the get value will be filled

          * otherwise it will be NULL (like clicking the calendar link in main menu) and the

          * class will account for this and set the default current month and year.

          */

         $month = (isset($_POST['month'])) ? $_POST['month'] : $_GET['month'];

         $year = (isset($_POST['year'])) ? $_POST['year'] : $_GET['year'];

        

         // If the get requests don't have anything, NULL values will be assigned

         $calendar=new Calendar($month,$year);

        

         // Create links for the control menu

         $calendar->nextMonthUrl = $this->createUrl('site/calendar', array(

             'month'=>$calendar->nextMonth,

             'year'=>$calendar->yearNextMonth)

         );

         $calendar->previousMonthUrl = $this->createUrl('site/calendar', array(

             'month'=>$calendar->previousMonth,

             'year'=>$calendar->yearPreviousMonth)

         );

         $calendar->storePreviousLink = CHtml::link("<< Previous Month", $calendar->previousMonthUrl);

         $calendar->storeNextLink = CHtml::link("Next Month >>", $calendar->nextMonthUrl);

         $this->render('showCalendar',array('calendar'=>$calendar));

     }

  

The showCalendar view




  <h2 align="center"><?php echo $calendar->title; ?></h2>

<?php echo $calendar->printStartForm(); ?>

<?php echo $calendar->storePreviousLink; ?>

  .::

<?php echo $calendar->printControlMenu();?>

::.

<?php echo $calendar->storeNextLink; ?>

<?php echo $calendar->printCloseForm(); ?>

<?php echo $calendar->printCalendar(); ?>

  

Now if you weren’t going to use the Yii framework for this, then all the code is still usable (as a normal class file, just include it to your site and create a new instance)

The only thing is you’ll have to setup the controls menu for proper use with links post/get. Since Yii makes it’s own html links I followed it’s format but with a normal php application you’d have to define links like normal <a href…></a>

Just search inside the Calendar class within the setControlMenu function for:




         /* "next month" control and "previous month" control */

         /* For plain old PHP

         $this->nextMonth = '<a href="?month='.($this->month != 12 ? $this->month + 1 : 1).

         '&year='.($this->month != 12 ? $this->year : $this->year + 1).'" class="control">Next Month >></a>';

         $this->previousMonth = '<a href="?month='.($this->month != 1 ? $this->month - 1 : 12).

         '&year='.($this->month != 1 ? $this->year : $this->year - 1).'" class="control"><<     Previous Month</a>'; */

        

         // For Yii Framework

         $this->nextMonth = ($this->month != 12 ? $this->month + 1 : 1);

         $this->yearNextMonth = ($this->month != 12 ? $this->year : $this->year + 1);

         $this->previousMonth = ($this->month != 1 ? $this->month - 1 : 12);

         $this->yearPreviousMonth = ($this->month != 1 ? $this->year : $this->year - 1);    

  

That should help you with how the links are displayed.

I hope everyone finds use for this :) and I’ll see about getting an actual live site demo showing.

But try it out, works like a charm and it’s very extensible and reusable :) .

Ok got a viewable demo: http://www.sterlings…rials/calendar/

Now the change you have to make is simply in the calendar.php file - aside from that the code is shown on that site - for just regular php applications. If you want to use the Yii framework - just use the code in the above posts.

To make links and presentation work for free form PHP




 <?php

          /* "next month" control and "previous month" control */

          // For plain old PHP

          $this->nextMonth = '<a href="?month='.($this->month != 12 ? $this->month + 1 : 1).

          '&year='.($this->month != 12 ? $this->year : $this->year + 1).'" class="control">Next Month >></a>';

          $this->previousMonth = '<a href="?month='.($this->month != 1 ? $this->month - 1 : 12).

          '&year='.($this->month != 1 ? $this->year : $this->year - 1).'" class="control"><<     Previous Month</a>';

         

          /* For Yii Framework

          $this->nextMonth = ($this->month != 12 ? $this->month + 1 : 1);

          $this->yearNextMonth = ($this->month != 12 ? $this->year : $this->year + 1);

          $this->previousMonth = ($this->month != 1 ? $this->month - 1 : 12);

          $this->yearPreviousMonth = ($this->month != 1 ? $this->year : $this->year - 1);    */

         

          /* Bring all the controls together in a menu */

          $this->startForm = '<table cellpadding="0" cellspacing="0" class="'. $this->style .'-control"><tr><td><form method="post">';

          $this->displayControls = $this->previousMonth. ' .::'. $this->selectMonth.$this->selectYear.

              ' <input type="submit" name="submit" value="Go" /> ::. '. $this->nextMonth;

          $this->closeForm = '</form></td></tr></table>';

 ?>

 

Enjoy :D

Nice! I found David’s site and was about to do this myself… but now I don’t have to! Thanks!

Haha ya, well truth be told I’m using a lot of your own work to learn about the Yii framework and develop applications as needed. Seeing how you implemented your time component, gave me the idea to run this as a calendar one - so it’s actually built right into your skeleton right now lol. I actually made an addition to your time component with a validation setup checking if a date submitted is valid. Later I’ll build a custom rules validation component and transfer it there so proper usage.

The next feature will be making a custom search, where you get to chose what sections of the site are available to be searched. i.e. if you run a site that stores client messages and data between you and the client (for your business) then you won’t want that information shared and available with a site search feature.

I have a template just need to start working on it. I like you’re registration and email / forgot password setup so I’ll be going through it to break it down so I can explain it then I’ll use it as my basis for my documented guide.

Once I complete all these “templates” I’ll be able to design sites on the fly with a variety of features and I’ll even walk through a whole guide - more like a book lol - on the site designing aspect explaining each part of these features I use and how to implement them with the Yii framework.

It’s a huge project sure but it’ll be worth it, and I’ll come out on top knowing the framework inside and out.

Nice! if you release it to the public I will be one of the first to check it out!

One thing I still need to implant in my Time class is allowance for Unix timestamps to be passable to them. I’ve been working on a client’s website using my skeleton app and while doing so I’ve extended a lot of features that were originally designed for the skeleton app (but I extended them within the client’s app). Now I need to transfer them from the client’s website to the skeleton app.

hi,

this is very useful for me, as I need to implement an agenda … what about releasing it as a yii extension ? ;)

8)

putting it up on google code or another SVN host would be nice

Feel free to include it part of yours with the skeleton app or as a side project to it. I don’t have any google code setup. I’ll have to eventually look into something like that.

As for a yii extension I haven’t taken the time to learn about doing that yet. Maybe when I get a chance I will. I prefer to write snippets and tutorials so people can actually learn from it not just use it.

simply GREAT!

First of all this is really cool! I was looking for an example of a PHP calendar and found your site.

I am very new to PHP and server side programming but have been programming for many years, so please forgive me if this is something simple. My site has PHP Version 5.2.2 installed on it and I have created a test page for the calendar ( http://www.caddee.com/calendar/ ), but when I try to advance to the next/previous month using the hyperlink nothing happens, and if I use the Option pop-down to change month or year I receive an error message “HTTP 405 Method Not Allowed” when I click the ‘GO’ button.

Does anyone know why I get this error?

Thank you for any help.

Jim

By the looks of your directory setup it seems the html page is what runs and then you try to include the php file which won’t work.

Your html file actually needs to have the extension php in order to work the php code. PHP files can have html in them so don’t worry about that issue. But HTML files will not read PHP code properly so you can’t go both ways.

If you still have issues, post the the code to the page that displays the calendar.

Thank you for your quick reply.

I’m afraid I seem to be creating more problems for myself. Below is the code for my PHP file. I can’t get the Next and Previous months links to show, the Options don’t function and September 1st is showing on Monday instead of Tuesday.

Any ideas where I went wrong? http://www.caddee.com/calendar/test.php

Thank you again for your help.

Jim

<?php

include(&quot;calendar.php&quot;);


&#036;month = (isset(&#036;_POST['month'])) ? &#036;_POST['month'] : &#036;_GET['month'];


&#036;year = (isset(&#036;_POST['year'])) ? &#036;_POST['year'] : &#036;_GET['year']

?>

<?php $calendar = new Calendar(); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;


&lt;title&gt;Sample Calendar Application - Designed as an OOP Class&lt;/title&gt;


&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;calendar.css&quot;&gt;

</head>

<body>

&lt;center&gt;


	&lt;h2 align=&quot;center&quot;&gt;&lt;?php echo &#036;calendar-&gt;title; ?&gt;&lt;/h2&gt;


	&lt;?php echo &#036;calendar-&gt;printStartForm(); ?&gt;


	&lt;?php echo &#036;calendar-&gt;storePreviousLink; ?&gt;


	.::


	&lt;?php echo &#036;calendar-&gt;printControlMenu();?&gt;


	::.


	&lt;?php echo &#036;calendar-&gt;storeNextLink; ?&gt;


	&lt;?php echo &#036;calendar-&gt;printCloseForm(); ?&gt;


	&lt;?php echo &#036;calendar-&gt;printCalendar(); ?&gt;


&lt;/center&gt;

</body>

</html>

Ok so that explains it ;) you’re using (which makes sense because you’re on this forum haha) the Yii framework version of the application. I designed the calendar with muli-functionality.

If you haven’t already I would highly suggest you take a look at the calendar.php file with the calendar class in it so you can see how you can customize the calendar for yourself.

But on a simplicity level, here’s what my code is for just free form php (not for the framework).




<?php 


include("calendar.php");


/*

 * If the user selected the month and year via drop down then it's set

 * via post - otherwise they clicked a link, which means it's set by get.

 * If the next / previous month links were clicked the get value will be filled

 * otherwise it will be NULL (like clicking the calendar link in main menu) and the

 * class will account for this and set the default current month and year.

 */

$month = (isset($_POST['month'])) ? $_POST['month'] : $_GET['month'];

$year = (isset($_POST['year'])) ? $_POST['year'] : $_GET['year'];




// If the get requests don't have anything, NULL values will be assigned

$calendar=new Calendar($month,$year);


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Sample Calendar Application - Designed as an OOP Class</title>

<link rel="stylesheet" type="text/css" href="calendar.css">

</head>

<body>


<h2 align="center"><?php echo $calendar->title; ?></h2>

<?php echo $calendar->printStartForm(); ?>

<?php echo $calendar->printControlMenu();?>

<?php echo $calendar->printCloseForm(); ?>

<?php echo $calendar->printCalendar(); ?>

</body>

</html>



That’s it, now I also swapped inside the calendar.php file the calendar class information for the control menu. If you open up the calendar.php file and go to line: 212 you should see some documentation that looks like this:




/* "next month" control and "previous month" control */

        // For plain old PHP

        $this->nextMonth = '<a href="?month='.($this->month != 12 ? $this->month + 1 : 1).

        '&year='.($this->month != 12 ? $this->year : $this->year + 1).'" class="control">Next Month >></a>';

        $this->previousMonth = '<a href="?month='.($this->month != 1 ? $this->month - 1 : 12).

        '&year='.($this->month != 1 ? $this->year : $this->year - 1).'" class="control"><<     Previous Month</a>'; 

        

        /* For Yii Framework

        $this->nextMonth = ($this->month != 12 ? $this->month + 1 : 1);

        $this->yearNextMonth = ($this->month != 12 ? $this->year : $this->year + 1);

        $this->previousMonth = ($this->month != 1 ? $this->month - 1 : 12);

        $this->yearPreviousMonth = ($this->month != 1 ? $this->year : $this->year - 1);    */



Because this was placed on the Yii framework forum, that was the first choice, so in your calendar.php file it’s probably set to use the Yii framework setup for the control menu. All you do is comment that out and uncomment the one for the plain php and it should work :).

Sweet! It’s working now.

There are a couple of things that happened that I didn’t expect and I was wondering if you are experiencing the same.

When I first go to my test calendar, September is the current month but the first day of the month is on Monday instead of Tuesday and if I go to the next month and then back it’s correct. I get the same result on your demo page http://www.sterlingsavvy.com/tutorials/calendar/.

The other thing I did not expect, is when switching to the next or previous month, the current-day number is highlighted for that month too. I personally would prefer to have only the current day for the current month highlighted and I think it would go something like this (?)…

     /* keep going with days.... */


     for(&#036;list_day = 1; &#036;list_day &lt;= &#036;days_in_month; &#036;list_day++):


         if((&#036;list_day == date(&quot;j&quot;,mktime(0,0,0,&#036;this-&gt;month))) &amp;&amp; (date(&quot;F&quot;) == date(&quot;F&quot;,mktime(0,0,0,&#036;this-&gt;month)))) [b]&lt;&lt;&lt;&lt;&lt; - I added another condition to compare the current month with this-&gt;month.[/b]


         {    


             &#036;this-&gt;calendar.= '&lt;td class=&quot;'. &#036;this-&gt;style .'-current-day&quot;&gt;';


         }


         else            


         {…

This seems to work but I’m not really sure if I accomplished my goal in the best way.

Thanks again for all your help.

Jim

PS. I Just realized I should test for the year too. So I changed the conditional test to…

if(($list_day == date("j",mktime(0,0,0,$this->month))) &&

(date("F") == date("F",mktime(0,0,0,$this->month))) &&

(date("Y") == date("Y",mktime(0,0,0,$this->month,$list_day,$this->year))))

{

Well of course feel free to modify and manipulate it - that’s the purpose of making it a class so it can be reused and modified effectively without destroying all the other code in your site.

As for the day of the month issue, could just be a bug with September. It wasn’t doing that sort of nonsense in August but I haven’t touched it much since I made it so it’s quite possible there’s a serious bug that needs to be fixed. In which case I’ll check next month and if it does the same thing then I’ll look into it.

But no please feel free to modify or make any additions and even share them here on the forum as it only adds to the community in what people can do and have ready to go for them to manipulate. I just turned a simple calendar with some average features into a class setup to be reused, by all means is it not perfect and of course can be improved or customized for each individual purpose :)

Hope it serves you well however. Give it another week and then I’ll get back here if the month issue occurs with October. Thanks for the notice of the issue however. It was something I noticed on rare occasions but only for September (when I was switching from August to September) and could not always replicate it so never figured it an issue with the code but it’s quite possible.

First time I’ve tried posting so hope it works. I think the day of the month issue is caused by this code.


 

/* We need to take the month value and turn it into one without a leading 0 */

         if((substr($this->month, 0, 1)) == 0)

         {

             // if value is between 01 - 09, drop the 0

             $tempMonth = substr($this->month, 2); //The 2 should be 1 if you want the second character.

             $this->month = $tempMonth;

         }



I didn’t try it so its not tested.