Return to Snippet

Revision: 21157
at December 4, 2009 15:11 by SmpleJohn


Initial Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Calendar</title>

<style type='text/css'>
	#calendar {width:900px; overflow:auto; margin:0 auto;}
		li {width:14%; height:120px; display:block; position:relative; float:left; border:#000 1px solid; border-width:1px 0 0 1px; overflow:hidden;}
		.last {border-right-width:1px;}
		.lastweek {border-bottom-width:1px;}
		.today {background:#ccc;}
		.today .date {background:#333; color:#fff; font-weight:bold;}
			.date {display:block; width:20px; height:20px; position:relative; top:0; left:0; text-align:center; border:#000 1px solid; border-width:0 1px 1px 0;}
			li p {padding:0 4px; margin:0;}
</style>

</head>

<body>

<div id='calendar'>
	<?php
        function calendar($setdate) {
            $boom = explode("/",$setdate);
            $m = $boom[0];
            $y = $boom[1];
            $totaldays = date("t", strtotime($y . "-" . $m . "-01"));
            $prev = date("m/y",mktime(0,0,0,$m-1,1,$y));
            $next = date("m/y",mktime(0,0,0,$m+1,1,$y));
            $days = range (1, $totaldays);
           
            $startday = date("w",mktime(0,0,0,$m,1,$y));
            $lastli = 6-(date("w",mktime(0,0,0,$m,$totaldays,$y)));
			$count = 1;
			$weeks = 1;
			$weekcount = ceil(($startday + $totaldays)/7);
			if($startday==0){
				$weekcount++;
			}
			
            echo "<div id='controls'><a href='?caldate=$prev' id='prevmonth' class='left'>$prev - prev</a> | $setdate | <a href='?caldate=$next' id='nextmonth' class='right'>next - $next</a></div>";
            for($i=0; $i<$startday; $i++){
                echo "<li class='blank'></li>";
				$count++;
            }
            foreach ($days as $value) {
				echo "<li class='";
				if ($count == 7){
                	echo "last ";
					$count = 1;
				} else if ($count == 1){
					$weeks++;
					$count++;
				} else {
					$count++;
				}
				if ($weeks == $weekcount){
					echo "lastweek";
				}
				if (date('y') == $y && date('m') == $m && date('j') == $value){
						echo " today";
				}
				echo "'><div class='date'>$value</div><p>Title 1<br />Title 2</p></li>";
            }
            for($i=0; $i<$lastli; $i++){
				if ($count == 7){
					echo "<li class='blank last lastweek'></li>";
					$count = 1;
				} else {
					echo "<li class='blank lastweek'></li>";
					$count++;
				}
            }
        }
    
        if (empty($_REQUEST['caldate'])){
            $date = date("m/y");
        } else {
            $date = $_REQUEST['caldate'];
        }
    
        calendar($date);
    ?>
</div><!-- calendar -->

</body>
</html>

Initial URL
http://smple.com/snipplr/calendar.php

Initial Description
This is using list items to create the calendar so it is very flexible with CSS. Right now it's just displaying dummy data, but you can easily integrate a mysql call into the loop.

Initial Title
Calendar Function

Initial Tags
php

Initial Language
PHP