PHP Function to Display Calendar of Current Month

by Sasikumar 2014-04-16 16:17:03

PHP function to display calendar of current month:
This functions helps to display calendar of current month or the calendar of the date passed to it,
function calendar($date)
{
if($date == null) $date = getDate();

$day = $date["mday"];
$month = $date["mon"];
$month_name = $date["month"];
$year = $date["year"];

$this_month = getDate(mktime(0, 0, 0, $month, 1, $year));
$next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year));

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

$calendar_html = "<table style="background-color:666699; color:ffffff;">";

$calendar_html .= "<tr><td colspan="7" align="center" style="background-color:#D3D7CF; color:000000;">" . $month_name . " " . $year . "</td></tr>";

$calendar_html .= "<tr>";

for($week_day = 0; $week_day < $first_week_day; $week_day++) { $calendar_html .= "<td style="background-color:9999cc; 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>";

if($day == $day_counter) $calendar_html .= "<td align="center">" . $day_counter . "</td>";
else $calendar_html .= "<td align="center" style="background-color:9999cc; color:000000;">" . $day_counter . " </td>";

$week_day++;
}

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

return($calendar_html);
}

Output:
Screenshot.png
597
like
0
dislike
0
mail
flag

You must LOGIN to add comments