Home arrow PHP arrow Page 4 - A Useful Event Calendar Written In PHP

A Useful Event Calendar Written In PHP

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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 198
June 10, 2002
  1. · A Useful Event Calendar Written In PHP
  2. · The event calendars logic
  3. · Creating the calendar
  4. · Highlighting days with events
  5. · Adding an event
  6. · Conclusion

print this article

A Useful Event Calendar Written In PHP - Highlighting days with events
(Page 4 of 6 )

As I mentioned earlier, days that have events attached to them are flagged with a blue background thanks to the use of some simple CSS styling. We have a function called ReadEvents that opens the events text file and reads each event into a multi-dimensional associative array. This array is then dissected and checked for events that match each particular day of the month.

ReadEvents accepts one parameter, which is the month to retrieve all events for:

function ReadEvents($Month)
// We will get all of the events for this month into an associative
// array and that array will then be returned

$theEvents = array();
$eventCounter = 0;

// Make sure that the file exists
if(!file_exists($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE))
$fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE, "w")
or die("<span class='error'>ERROR: Couldn't create events file.</span>");

$fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE, "rb")
or die("<span class='error'>ERROR: Couldn't open events file to read events.</span>");

We make sure that the events file exists with the use of file_exists. If you receive errors here then make sure that the directory where the event file exists is CHMOD'ed with write attributes.

In the code above we use PHP's DOCUMENT_ROOT server variable to make sure that we have the correct path to the events file. The location of the events file is defined at the top of cal.php as the EVENT_FILE constant and looks like this:

define("EVENT_FILE", "cal_events.text");

You can of course change this to whatever you like. Once we've opened the events file, we use fread to read the contents of the file 1,024 bytes at a time into the $events variable:

while($data = fread($fp, 1024))
$events .= $data;


Remember that each event has three lines: the date it was posted, a name, and the actual description of the event. We use the explode function to grab all lines separated by the newline character into an array, like so:

// Seperate the data into line-seperated arrays
$arrEvents = explode("\r\n", $events);

Next up we have a for loop. This for loop increments through the $arrEvents array three lines at a time, which means that it only checks the date of each event. It uses the explode function to get the month, day and year of each event into an array called $arrEventDate.

With this array it compares the month that the event was posted to the current month selected on the calendar. If they are the same then that event is added to the $theEvents variable as an associative array, like this:

// Loop through the results and pick the arrays
// that match the selected month
for($i = 0; $i < sizeof($arrEvents); $i+=3)
// Get each part of the events date as an index
// of an array
$arrEventDate = explode(" ", $arrEvents[$i]);

// If the month is the selected month the grab
// the details of this event
if((int)$arrEventDate[0] == (int)$Month)
$theEvents[$eventCounter++] = array("day" => $arrEventDate[1], "name" => $arrEvents[$i+1], "desc" => $arrEvents[$i+2]);

Lastly, we return the array:

return $theEvents;

We now have an associative array that contains all events posted for the selected month. We use PHP's foreach construct as well as several if statements to loop through each event and determine which day is was posted for. We then setup the appropriate style for that day's table cell in the calendar table:

foreach($arrEvents as $eventEntry)
if($eventEntry["day"] == $i)
// We have at least one event for the day
$hasEvent = true;


// Is it a weekend, does it have events, etc
if($i == $day && $month == date("m") && $year == date("Y") && $sel == 1)
echo "class='selected'";
else if($i == $day && $sel == 1)
echo "class='selected'";
else if($hasEvent == true)
echo "class='event'";
if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)
echo "class='weekend'";
if($i == date("d") && $month == date("m") && $year == date("Y"))
echo "class='today'";
echo "class='normal'";

Browsing by day
OK, we can now show each month on the calendar, but we also need to show each day on the calendar as well as the events attached to each day. To do this, we setup each day of the calendar as a hyperlink. Something like this:


The key here is the sel=1 query string variable. We use this to flag that a day is selected and display the events for that day, like so:

$arrEvents = ReadEvents($month);


foreach($arrEvents as $eventEntry)
if($eventEntry["day"] == $i)
// We have at least one event for the day
$hasEvent = true;

if($eventEntry["day"] == $day)
// Add the event to the $todaysEvents variable
$todaysEvents .= "<span class='eventTitle'>" . stripslashes($eventEntry["name"]) . "</span>";

$todaysEvents .= "<span class='eventDesc'><br>" . stripslashes($eventEntry["desc"]) . "</span><br>";

$todaysEvents .= " <a href='cal.php?sel=1&what=delPost&day=$day&month=$month&year=$year&eName=" . urlencode($eventEntry["name"]) . "&eDesc=" . urlencode($eventEntry["desc"]) . "'>[Remove]</a><br><br>";

After the for loop, we have a variable called $todaysEvents that contains the HTML code for all events on the selected day. We then use an if statement to show the events under the calendar:

if($sel == 1)
echo "<tr>";
echo " <td width='350' colspan='7'>";
echo " <hr size='1' color='#CACACA' noshade>";
echo " <span class='Title'>Today's Events</span><br><br>";
echo $todaysEvents;
echo " </td>";
echo "</tr>";

Here's how a day looks with some events attached to it:

Events attached to a particular day
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials