An events calendar allows you to keep a schedule of your appointments, etc. In this article Mitchell creates an extremely useful events calendar using only PHP.
A Useful Event Calendar Written In PHP - The event calendars logic (Page 2 of 6 )
As the name suggests, an events calendar stores events. These events are shown along with the calendar when a specific day is selected, such as the 11th June, 2002. The calendar will display events in a "by month" format, meaning that each of the thirty or so days for a month will be shown on one page. If any of the days have events "attached" to them, then that day will be highlighted in blue. Likewise, today's date will be highlighted in grey, and the currently selected day (if any) will be highlighted in red.
If we click on a day, then all of the events attached to that day will be displayed, along with a form through which we can add a new event. All events include a link to remove them.
So, just to get you excited, here's what the calendar will look like when we're finished:
Today is the 11th, so see how June 11th is highlighted in grey? The PHP code to drive our calendar will be stored in one file called cal.php. Cal.php will work with a flat text file called cal_events.text. Cal_events.text will contain the list of events for our calendar. Each event is stored in the following format:
So if I had an event reminding me that today is the 11th of June, then it would be stored in cal_events.text like this:
06 11 2002 Today is the 11th Of June! Guess what, today is the 11th of June. Woohoo!
Each entry takes up three rows, and we can use PHP's powerful file and array functions to effortlessly grab each event from the flat file and display/delete it accordingly.
Cal.php is separated into three different sections:
Add Post: The AddPost() function allows us to add an event to a specific day of our calendar.
Delete Post: The DelPost() function allows us to delete an event from our calendar.
Display Calendar: We display the calendar by making use of PHP’s powerful date manipulation functions, by using for loops, and also by opening the events file and flagging particular days that contain events, etc.
Each day in a particular month is represented as part of a table as a <td> cell. The calendar was designed to take advantage of Internet Explorer's Cascading Style Sheet (CSS) abilities, so at the top of cal.php I have defined some styles for various parts of the calendar:
This style is used to highlight the current day, making it easy to work out what the day is and how many days there are until the end of the month, etc.
Lastly we have the event style, which is used for any day that is attached to one/more events from the events file.
After adding a couple of events and selecting a day, here's how our calendar will look:
In the screen shot above, days 12, 24 and 28 contain events. It is the 11th of June, so the 11th is highlighted in grey, and we have the 12th day selected, so it is highlighted in red. Note the events for the 12th are shown under the "Today's Events" section just under the calendar.
Well now that we know a bit more about how the calendar functions, let's jump right in and take a look at how to code up the calendar.