Calendar Script
Original posted by: Admin on 10-03-2007
Have you ever wanted to put your own calendar on a website?
Now you can thanks to this easy to use calendar script. It uses div's for the layout which makes it extremely flexible. You can also choose which day, month and year you would like to display.
function calendar($day, $month, $year, $array = array()) {
//Check the date is valid
if(!checkdate($month, $day, $year))
return false;
//Create the standard table
echo '<div id="calendar">';
//Print the month and year
echo '<div class="header">'.date('F - Y', mktime(0, 0, 0, $month, 1, $year)).'</div>';
//Print out all the days (Starting on Sunday)
echo '<div class="date">S</div>';
echo '<div class="date">M</div>';
echo '<div class="date">T</div>';
echo '<div class="date">W</div>';
echo '<div class="date">T</div>';
echo '<div class="date">F</div>';
echo '<div class="date">S</div>';
//What day does the month start on?
$start_day = date('w', mktime(0, 0, 0, $month, 1, $year));
//What day does the month end on?
$end_day = date('w', mktime(0, 0, 0, $month+1, -1, $year));
//How many days are in the month?
$days_in_month = date('t', mktime(0, 0, 0, $month, 1, $year));
//Loop through each day
for($i = 0; $i < $days_in_month+$start_day; $i++) {
$date = (($i - $start_day)+1);
//Is today the selected date?
if($date == $day)
echo '<div class="selected">';
else
echo '<div class="date">';
//Is their a link for todays date?
if(isset($array[$date]) && !empty($array[$date]))
echo '<a href="'.$array[$date].'" class="active">';
//Print the date number, or a space
//if its the start or end of the month
if($i >= $start_day) {
echo $date;
} else {
echo ' ';
}
if(isset($array[$date]) && !empty($array[$date]))
echo '</a>';
echo '</div>';
unlink($date);
}
//Print remaining days
for($i = $end_day; $i < 5; $i++)
echo '<div class="date"> </div>';
echo '</div>';
}
You must send the calendar the day, month and year that you would like to display, make sure you pass it a valid int.
calendar(15, 1, 2007);
//OR
calendar(date('d'), date('m'), date('Y'));
If you want a certain day to link somewhere then be sure to pass the function an array.
$myArray[15] = 'http://www.mywebsite.com';
calendar(15, 1, 2007, $myArray);
An example of the calendar function in action can be seen here
The styles that I used are as follows
<style>
#calendar .date a.active {
display: block;
width: 25px;
height: 25px;
padding:0;
margin:0;
float:left;
position:relative;
color: #FFFFFF;
background:#DD3737;
}
#calendar .selected {
display: block;
width: 25px;
height: 25px;
margin:0;
padding:0;
border:#DD3737 1px solid;
border-spacing: 5px;
float:left;
position:relative;
text-align: center;
vertical-align: middle;
}
#calendar .date {
display: block;
width: 25px;
height: 25px;
margin:0;
padding:0;
border:#FFFFFF 1px solid;
border-spacing: 5px;
float:left;
position:relative;
text-align: center;
vertical-align: middle;
}
#calendar {
display: block;
width: 190px;
padding:0;
margin:0;
}
#calendar .header {
display: block;
width: 190px;
text-align: center;
color: #FF0000;
margin:0;
padding:0;
}
</style>
Enjoy, if you can think of anything that would make this calendar better please let me know!