How to make PHP Calendar using HTML and CSS

In most of web sites and portal we need to show the calendar. If you want to show the calendar dynamically we can obtain that by using Jquery or PHP. In this article I will explain you how to create a stunning HTML, CSS and PHP calendar. You need to call calendar function with the value for which month and year’s calendar you want to create. I have given the PHP calendar Script download in this article.

How to create HTML and PHP Calender

You can view demo and download the PHP HTML calendar below:

Live Demo Download

Now we will create a function which will generate the calendar for the given month and year. Initially you need to declare the days in week, HTML table header etc.. Find the PHP script below..

//Draw Calendar
function draw_calendar($month,$year){

	// Draw table for Calendar 
	$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

	// Draw Calendar table headings 
	$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

	//days and weeks variable for now ... 
	$running_day = date('w',mktime(0,0,0,$month,1,$year));
	$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
	$days_in_this_week = 1;
	$day_counter = 0;
	$dates_array = array();

	// row for week one 
	$calendar.= '<tr class="calendar-row">';

	// Display "blank" days until the first of the current week 
	for($x = 0; $x < $running_day; $x++):
		$calendar.= '<td class="calendar-day-np">Â </td>';

	// Show days.... 
	for($list_day = 1; $list_day <= $days_in_month; $list_day++):
		if($list_day==date('d') && $month==date('n'))
		$calendar.= '<td class="calendar-day '.$currentday.'">';
			// Add in the day number
			if($list_day < date('d') && $month==date('n'))
				$showtoday='<strong class="overday">'.$list_day.'</strong>';
			$calendar.= '<div class="day-number">'.$showtoday.'</div>';

		// Draw table end
		$calendar.= '</td>';
		if($running_day == 6):
			$calendar.= '</tr>';
			if(($day_counter+1) != $days_in_month):
				$calendar.= '<tr class="calendar-row">';
			$running_day = -1;
			$days_in_this_week = 0;
		$days_in_this_week++; $running_day++; $day_counter++;

	// Finish the rest of the days in the week
	if($days_in_this_week < 8):
		for($x = 1; $x <= (8 - $days_in_this_week); $x++):
			$calendar.= '<td class="calendar-day-np">Â </td>';

	// Draw table final row
	$calendar.= '</tr>';

	// Draw table end the table 
	$calendar.= '</table>';
	// Finally all done, return result 
	return $calendar;

Now we all set to go.. Call the above function with the value of Month and Year. Find the example below how to call.

/* Show Calendar */
echo '<h2>January 2013</h2>';
echo draw_calendar(1,2016);

Now run the script. You can see the calendar for the given month and year. You can style the calendar using CSS. View demo for CSS styled Calendar.

1 Comment How to make PHP Calendar using HTML and CSS

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>