Categories

Tools

bitdefender

1&1 Web Hosting

How to create a monthly calendar with PHP


The example below allows you to create a simple monthly calendar using PHP. You can highlight the individual days of the month with different colors.

<style type="text/css">
	table  {
		border:1px solid #aaa;
		border-collapse:collapse;
		background-color:#fff;
		font-family: Verdana;
		font-size:12px;
	}
	
	th {
		background-color:#777;
		color:#fff;
		height:32px;
	}
	
	td {
		border:1px solid #ccc;
		height:32px;
		width:32px;
		text-align:center;
	}
	
	td.red {
		color:red;
	}
	
	td.bg-yellow {
		background-color:#ffffe0;
	}
	
	td.bg-orange {
		background-color:#ffa500;
	}
	
	td.bg-green {
		background-color:#90ee90;
	}
	
	td.bg-white {
		background-color:#fff;
	}
	
	td.bg-blue {
		background-color:#add8e6;
	}
	
	a {
		color: #333;
		text-decoration:none;
		
	}
</style>


<table border='0' >
<?php 
// Get the current date
$date = getdate();

// Get the value of day, month, year
$mday = $date['mday'];
$mon = $date['mon'];
$wday = $date['wday'];
$month = $date['month'];
$year = $date['year'];


$dayCount = $wday;
$day = $mday;

while($day > 0) {
	$days[$day--] = $dayCount--;
	if($dayCount < 0)
		$dayCount = 6;
}

$dayCount = $wday;
$day = $mday;

if(checkdate($mon,31,$year))
	$lastDay = 31;
elseif(checkdate($mon,30,$year))
	$lastDay = 30;
elseif(checkdate($mon,29,$year))
	$lastDay = 29;
elseif(checkdate($mon,28,$year))
	$lastDay = 28;

while($day <= $lastDay) {
	$days[$day++] = $dayCount++;
	if($dayCount > 6)
		$dayCount = 0;
}	

// Days to highlight
$day_to_highlight = array(8, 9, 10, 11, 12, 22,23,24,25,26);

echo("<tr>");
echo("<th colspan='7' align='center'>$month $year</th>");
echo("</tr>");
echo("<tr>");
	echo("<td class='red bg-yellow'>Sun</td>");
	echo("<td class='bg-yellow'>Mon</td>");
	echo("<td class='bg-yellow'>Tue</td>");
	echo("<td class='bg-yellow'>Wed</td>");
	echo("<td class='bg-yellow'>Thu</td>");
	echo("<td class='bg-yellow'>Fri</td>");
	echo("<td class='bg-yellow'>Sat</td>");
echo("</tr>");

$startDay = 0;
$d = $days[1];

echo("<tr>");
while($startDay < $d) {
	echo("<td></td>");
	$startDay++;
}

for ($d=1;$d<=$lastDay;$d++) {
	if (in_array( $d, $day_to_highlight))
		$bg = "bg-green";
	else
		$bg = "bg-white";
	// Highlights the current day	
	if($d == $mday)
		echo("<td class='bg-blue'><a href='#' title='Detail of day'>$d</a></td>");
	else 
		echo("<td class='$bg'><a href='#' title='Detail of day'>$d</a></td>");


	$startDay++;
	if($startDay > 6 && $d < $lastDay){
		$startDay = 0;
		echo("</tr>");
		echo("<tr>");
	}
}
echo("</tr>");
?>
</table>

 

Here is the result of the example:

How to create a monthly calendar with PHP

Posted in PHP by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.