Hi,
I am attempting to code my own php events calendar myself and i emphasise the attempting. I have the following code so far!
now what i am hoping to achieve is to have a small calendar displayed with any events for the elected day shown underneath which i think is possible although i ahve not achieved it yet! if anyone can help would be grateful![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
I am attempting to code my own php events calendar myself and i emphasise the attempting. I have the following code so far!
Code:
<style>
.normal {
font-family: Verdana;
font-size: 8pt;
color: #000000;
height: 20;
padding-left: 5pt;
}
.today {
font-family: Verdana;
font-size: 8pt;
color: #FFFFFF;
background-color: #CACACA;
height: 20;
padding-left: 5pt;
}
.selected {
font-family: Verdana;
font-size: 8pt;
color: #FFFFFF;
background-color: #C00000;
height: 20;
padding-left: 5pt;
}
.event {
font-family: Verdana;
font-size: 8pt;
color: #000000;
background-color: #C6D1DC;
height: 20;
padding-left: 5pt;
}
</style>
<?php
// Get values from query string
$day = $_GET["day"];
$month = $_GET["month"];
$year = $_GET["year"];
$sel = $_GET["sel"];
$what = $_GET["what"];
if($day == "")
$day = date("j");
if($month == "")
$month = date("m");
if($year == "")
$year = date("Y");
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
$numEventsThisMonth = 0;
$hasEvent = false;
$todaysEvents = "";
<table width='350' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td class='head' width='50'>S</td>
<td class='head' width='50'>M</td>
<td class='head' width='50'>T</td>
<td class='head' width='50'>W</td>
<td class='head' width='50'>T</td>
<td class='head' width='50'>F</td>
<td class='head' width='50'>S</td>
</tr>
$numDays = date("t", $currentTimeStamp);
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
// Workout when the first day of the month is
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++)
echo "<td> </td>";
}
if($counter % 7 == 0)
echo "</tr><tr>";
echo "<td width='50'>$I</td>";
if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)
echo "class='weekend'";
else
if($i == date("d") && $month == date("m") && $year == date("Y"))
echo "class='today'";
else
echo "class='normal'";
$monthName = date("F", $currentTimeStamp);
?>
<tr>
<td width='50' colspan='1'>
<input type='button' value=' < ' onClick='goLastMonth(<?php echo $month . ", " . $year; ?>)'>
</td>
<td width='250' colspan='5'>
<span class='title'><?php echo $monthName . " " . $year; ?></span><br>
</td>
<td width='50' colspan='1' align='right'>
<input type='button' value=' > ' onClick='goNextMonth(<?php echo $month . ", " . $year; ?>)'>
</td>
</tr>
<?php
function goLastMonth(month, year)
{
// If the month is Januaru, decrement the year
if(month == 1)
{
--year;
month = 13;
}
document.location.href = 'cal.php?month='+(month-1)+'&year='+year;
}
function goNextMonth(month, year)
{
// If the month is December, increment the year
if(month == 12)
{
++year;
month = 0;
}
document.location.href = 'cal.php?month='+(month+1)+'&year='+year;
}
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>");
@fclose($fp);
}
$fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE, "rb")
or die("<span class='error'>ERROR: Couldn't open events file to read events.</span>");
define("EVENT_FILE", "cal_events.text");
while($data = fread($fp, 1024))
{
$events .= $data;
}
@fclose($fp);
// Seperate the data into line-seperated arrays
$arrEvents = explode("\r\n", $events);
// 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;
foreach($arrEvents as $eventEntry)
{
if($eventEntry["day"] == $i)
{
// We have at least one event for the day
$hasEvent = true;
$numEventsThisMonth++;
…
}
}
}
// 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'";
else
if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)
echo "class='weekend'";
else
if($i == date("d") && $month == date("m") && $year == date("Y"))
echo "class='today'";
else
echo "class='normal'";
cal.php?sel=1&day=11&month=6&year=2002
$arrEvents = ReadEvents($month);
foreach($arrEvents as $eventEntry)
{
if($eventEntry["day"] == $i)
{
// We have at least one event for the day
$hasEvent = true;
$numEventsThisMonth++;
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>";
}
}
}
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>";
}
?>
now what i am hoping to achieve is to have a small calendar displayed with any events for the elected day shown underneath which i think is possible although i ahve not achieved it yet! if anyone can help would be grateful