Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/webgalli/public_html/blog/wp-includes/plugin.php on line 571

Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/webgalli/public_html/blog/wp-includes/plugin.php on line 571
PHP code to create a calendar for the month - Team Webgalli Blog - Team Webgalli Blog
Request quote

PHP code to create a calendar for the month

Posted on: August 11th, 2012 by Mohammed Aqeel No Comments

This code snippet will help you to create a calendar through a few lines of php code which you can easily style with simple css and html. You can easily create your php web based calendar in just two steps. You may ask why this is needed when many calendar scripts are out. You are right, but sometimes you need to have an html calendar in your webpage.

Step 1 :  The calendar generation function

function php_calendar($month,$year){
	$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
	$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>';
	$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();
	$calendar.= '<tr class="calendar-row">';
	for($x = 0; $x < $running_day; $x++){
		$calendar.= '<td class="calendar-day-np"> </td>';
		$days_in_this_week++;
	}
	for($list_day = 1; $list_day <= $days_in_month; $list_day++){
		$calendar.= '<td class="calendar-day">';
		$calendar.= '<div class="day-number">'.$list_day.'</div>';
		$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++;
	};
	if($days_in_this_week < 8){
		for($x = 1; $x <= (8 - $days_in_this_week); $x++){
			$calendar.= '<td class="calendar-day-np"> </td>';
		};
	};
	$calendar.= '</tr>';
	$calendar.= '</table>';
	echo $calendar;
}

Step 2 : Show the calendar

Once you added the above function to your code’s core library you can show a calendar by simply using

<?php php_calendar(8,2012);?>

This will give you the calendar of August 2012 in your webpage.

CSS beautification

You can use the following css and customize the look and feel.

table.calendar{
	border-left:1px solid #999; 
	z-index:100;
}
tr.calendar-row{
}
td.calendar-day{ 
	min-height:10px; 
	font-size:10px; 
	position:relative; 
} 
* html div.calendar-day { 
	height:10px; 
}
td.calendar-day:hover{
}
td.calendar-day-np{
	background:#eee; 
	min-height:10px; 
} 
* html div.calendar-day-np { 
	height:10px; 
}
td.calendar-day-head { 
	background:#ccc; 
	font-weight:bold; 
	text-align:center; 
	width:100px; 
	padding:0px; 
	border-bottom:1px solid #999; 
	border-top:1px solid #999; 
	border-right:1px solid #999; 
}
div.day-number    { 
	background:#999; 
	padding:0px; 
	color:#fff; 
	font-weight:bold; 
	float:right; 
	margin:-5px -5px 0 0; 
	width:20px; 
	text-align:center; 
}
/* shared */
td.calendar-day, td.calendar-day-np {
	width:120px; 
	padding:5px; 
	border-bottom:1px solid #999; 
	border-right:1px solid #999; 
}

PS : Some of the codes are from the WWW. I forgot from where exactly I received those codes. Thanks to the author for that, just sharing the code hoping it will be useful for some one.

Don’t forget to share your thoughts below or suggestions.

Tags: , , , , , , , ,