Offering a calendar to your site is a popular and easy addition to any site. In this article I will explain the relatively simple logic to allow you to create your own calendar. To be more specific, we'll create a calendar which shows either a monthly or weekly view and assume you can pass variables into the program.
The first thing needed are the minimum variables or parameters necessary to give ourselves a fair amount of flexibility. These variables will be the starting day, number of days to show, and any offset needed. The first two variables are pretty obvious, but the purpose of offset may not be clear. offset can be used to show previous and next calendars while maintaining view (e. g. monthly or weekly).
Next, considering that we would like to be able to show periods such as “this week", “this month", “next week", etc. we will need to create some logic to handle these options. To do this, we'll have 3 variables the program will accept: calendar, showdays, & offset. calendar will be used to request a specific calendar (e. g. “this week") while the other two variables come from those defined above. Our script will need to take the calendar and offset variables to populate the starting day. Note: All code shown here is pseudo-code and will not work in any known language. For the example calendar script in PHP , please visit the author's site.
// First, establish the starting day and number of days to show.
// Express number of days using “month" for a monthly show because months vary in number of days.
IF calendar = “this month"
start_day = first of the month
num_days = “month"
ELSE IF calendar = “this week"
start_day = last Sunday
num_days = 7
. . . continue for all acceptable forms
start_day = default day
num_days = default number
// Now modify start_day by the appropriate offset and establish number of days (count_max) to
// show appropriate for the value of num_days
IF num_days = “month"
start_day = first of the month adjusted by offset number of months
count_max = number of days in the month
start_day = start_day adjusted by offset number of days
count_max = num_days
// Determine the day of the week start_day falls on
start_day_of_week = day of week for start_day, this should be a number from 0 to 6.
We have now collected all the information necessary to show the appropriate calendar. Moving on, we now need to generate our calendar. Our calendar should output the header information and the name of the month as a caption. Then all which remains is to output each row. Let's take a look at how that can be achieved:
column_position = start_day_of_week
IF column_position > 0
OUTPUT: empty TD cell spanning column_position+1 cells
original_start_day = start_day
current_day = start_day
WHILE (current_day - start_day) < count_max)
IF (column_position MOD 7) = 0
OUTPUT: open TR tag
OUTPUT: TD cell with current_day in it
IF (column_position MOD 7) = 0
OUTPUT: close TR tag
This will output each of the rows, wrapping on Saturday, but will leave the last TR tag open, so be sure to close it and the table. Then, all you need to do is save your file and use the code, setting the appropriate variables, wherever you would like! Now, this is an extremely simple example and adding features such as navigation and event management are definite pluses. For a copy of this script with functioning code , please see the author's website.
Jeremy Miller - Webmaster of Script Reference - The *NEW* PHP Reference & Tutorial Site For Non-Programmers