Wednesday, November 21, 2012

Simple PHP Calendar Code

PHP calendars can be very useful. You can do things as simple as showing the date, and as complex as an online booking system. Once you understand how to do this, you will be able to apply the same concepts to more complex calendars you may need.



<?php
function calendar($date)
{
//If no parameter is passed use the current date.
if($date == null){
$date = getDate();

}
$day = $date["mday"];
$month = $date["mon"];
$month_name = $date["month"];
$year = $date["year"];
if(isset($_GET['mday'])){
$day = $_GET["mday"];
$month = $_GET["mon"];
$month_name = $_GET["month"];
$year = $_GET["year"];
if($month == "0"){
$year = $year-1;
$num = cal_days_in_month(CAL_GREGORIAN, 12, $year ) ;
$day = $num;
$month = "12";

}
if($month == "13"){
$year = $year+1;
$num = cal_days_in_month(CAL_GREGORIAN,1, $year ) ;
$day = $num;
$month = "1";
}
$month_name = monthname($month);

}
$this_month = getDate(mktime(0, 0, 0, $month, 1, $year));
$next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year));
$prev_month1 = $month-1;
$next_month1 = $month+1;
$prev_month_name = monthname($prev_month1);
$next_month_name = monthname($next_month1);


//Find out when this month starts and ends.


$days_in_this_month = round(($next_month[0] - $this_month[0]) / (60 * 60 * 24));


$first_week_day = $this_month["wday"];
$calendar_html = "<table style=\"background-color:1865B1; color:ffffff;\" width='800' height='600'>";

$calendar_html .= "<tr><td colspan=\"7\" align=\"center\" style=\"background-color:7AB9E7; color:000000;\">"." <a href='unlock.php?mday=$day&mon=$prev_month1&month=$prev_month_name&year=$year'>Previous </a>" .
$month_name . " " . $year ." <a href='unlock.php?mday=$day&mon=$next_month1&month=$next_month_name&year=$year'>Next </a>"."</td></tr>";
$calendar_html .= "<tr><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thr</td><td>Fri</td><td>Sat</td></tr>";

$calendar_html .= "<tr>";


//Fill the first week of the month with the appropriate number of blanks.
for($week_day = 0; $week_day < $first_week_day; $week_day++)
{

$calendar_html .= "<td style=\"background-color:7AB9E7; color:000000;\"> </td>";
}

$week_day = $first_week_day;

for($day_counter = 1; $day_counter <= $days_in_this_month; $day_counter++)
{

$week_day %= 7;

if($week_day == 0)
$calendar_html .= "</tr><tr>";

//Do something different for the current day.
if(($day == $day_counter) && ($month == date("mon")) && ($year = date("Y")))
$calendar_html .= "<td align=\"center\"><b>" . $day_counter . "</b></td>";
else
$calendar_html .= "<td align=\"left\" style=\"background-color:7AB9E7; color:000000;\">&nbsp;" .
$day_counter ;

$week_day++;
}

$calendar_html .= "</tr>";
$calendar_html .= "</table>";

return($calendar_html);
}

function monthname($month){

switch($month){
case 1 : $month_name = 'January';
return $month_name;
break;
case 2 : $month_name = 'February';
return $month_name;
break;
case 3 : $month_name = 'March';
return $month_name;
break;
case 4 : $month_name = 'April';
return $month_name;
break;
case 5 : $month_name = 'May';
return $month_name;
break;
case 6 : $month_name = 'June';
return $month_name;
break;
case 7 : $month_name = 'July';
return $month_name;
break;
case 8 : $month_name = 'August';
return $month_name;
break;
case 9 : $month_name = 'September';
return $month_name;
break;
case 10 : $month_name = 'October';
return $month_name;
break;
case 11 : $month_name = 'November';
return $month_name;
break;
case 12 : $month_name = 'December';
return $month_name;
break;

}

}
echo $val = calendar(getDate());
?>