DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michael has posted 27 posts at DZone. View Full User Profile

Web Blog Calendar

12.14.2006
| 854 views |
  • submit to reddit
        // build_calendar function creates small blog calendar, only displays links on days that have entries in db
// uses index.php?m=12&y=2006&d=1

<?php
	function build_calendar($month,$year,$day,$todays_day) {
				/* Declaring the variables */
				$daysOfWeek = array('S','M','T','W','T','F','S');
				$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
				$noDays = date('t',$firstDayOfMonth);
				$dateComponents = getdate($firstDayOfMonth);
				$dayOfWeek = $dateComponents['wday'];
				$monthName = date('F',mktime(0,0,0,$month,1,$year));
				
				global $rsArticleDates_log;
				global $rsArticleDates_cal;
				global $_GET;
				
				if (mysql_num_rows($rsArticleDates_log) > 0)
				{
					mysql_data_seek($rsArticleDates_log,0);
					while($row_rsArticleDates_log = mysql_fetch_assoc($rsArticleDates_log)){
						$dates[] = $row_rsArticleDates_log['ArticleDate_log'];
					}
				}
				
				if (mysql_num_rows($rsArticleDates_cal) > 0)
				{
					mysql_data_seek($rsArticleDates_cal,0);
					while($row_rsArticleDates_cal = mysql_fetch_assoc($rsArticleDates_cal)){
						$dates_cal[] = $row_rsArticleDates_cal['ArticleDate_cal'];
					}
				}
				
				
				
				/* Computing the previous month. */
				if($month == 1) {
				 	$mn=12;
				 	$yn=$year-1;
					//$dy=31;
					$dy=1;
			 	} else {
				 		$mn=$month-1;
				 		$yn=$year;
						$dy=1;
				}
				
				/* Computing the next month. */
				if($month == 12) {
					$mn2=1;
					$yn2=$year+1;
					$dy=1;
				} else {
					$mn2=$month+1;
					$yn2=$year;
					$dy=1;
				}
				
				/* Calendar header: next and previous month links */
				/*
				$calendar = "<p class=about><table>";
				$calendar .= "<tr><td><a href=index.php?m=$mn&y=$yn&d=$dy><</a></td>";
				$calendar .="<td colspan=5><center>$monthName, $year</center></td>";
				$calendar .="<td><a href=index.php?m=$mn2&y=$yn2&d=$dy>></a></td></tr>";
				$calendar .="<tr>"; 
*/	

				//$calendar = "<p class=about><table>";
				$calendar .= "<h2><center><a href=index.php?m=$mn&y=$yn&d=$dy><</a> ";
				$calendar .="$monthName, $year";
				$calendar .=" <a href=index.php?m=$mn2&y=$yn2&d=$dy>></a></center></h2>";
				$calendar .="<p class=about><table><tr>"; 	

				//$date2 = $yn2."-".$mn2."-".$dy;
				/* Calendar header: Display the days of the week */
				foreach($daysOfWeek as $day) { 
				
					  
			          $calendar .= "<td><center><strong>$day</strong></center></td>"; 
					
				} 
				$calendar .= "</tr>"; 
				$calendar .= "<tr>"; 

			  $currentDay = 1;
			 

			  /* Fill in the beginning of the calendar	body */    
			  if ($dayOfWeek > 0) {  
			     $calendar .= "<td  colspan='$dayOfWeek'> </td>";  
			  } 
			
			  /* Generate the calendar body */		
			  while ($currentDay <= $noDays) { 
			        if ($dayOfWeek == 7) { 
            		   $dayOfWeek = 0; 
		               $calendar .= "</tr><tr>"; 
			        } 
					$date = $year."-".$month."-".$currentDay;
					if ((in_array($date,$dates)) || (in_array($date,$dates_cal)))
					{ 
						
						if ($date == date('Y-m-d'))
						{
						  $calendar .= "<td bgcolor=D8E5F2><strong><center><a href='index.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></center></strong></td>"; 
						 
						}
						else
						{
						  $calendar .= "<td><strong><center><a href='index.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></center></strong></td>"; 
						
						}
						//$calendar .= "<td><strong><center><a href='index.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></center></strong></td>"; 
						
					} else {
						
						
						if ($date == date('Y-m-d'))
						{
						$calendar .= "<td bgcolor=D8E5F2><center>$currentDay</center></td>";
						}
						else
						{
						$calendar .= "<td><center>$currentDay</center></td>";
						}
					
						//$calendar .= "<td>$currentDay</td>";
						$date = $year."-".$month."-".$currentDay;
						if ((in_array($date,$dates)) || (in_array($date,$dates_cal)))
						{ 
							
							
							if ($date == date('Y-m-d'))
							{
								$calendar .= "<td bgcolor=D8E5F2><strong><a href='index.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></strong></td>"; 
							
							}
							else
							{
							
								$calendar .= "<td><strong><a href='index.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></strong></td>"; 
							
							}
							//$calendar .= "<td><strong><a href='index.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></strong></td>"; 
							
						} 
						else 
						{
							//$calendar .= "<td><center>$currentDay</center></td>";
						}

					
					}
				   $currentDay++; 
				   $dayOfWeek++; 
			  } 

			/* Filling in the end of the calendar body */
			if ($dayOfWeek != 7) {  
		          $remainingDays = 7 - $dayOfWeek; 
        		  $calendar .= "<td colspan='$remainingDays'> </td>";  
			} 
  
  			$calendar .= "</table></p>"; 
			return $calendar;
} 

if (isset($_GET['m']) && isset($_GET['y']) && isset($_GET['d'])){ 
$month = $_GET['m'];
$year = $_GET['y'];
$day = $_GET['d'];
} else {
$dateComponents = getdate();
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
$day = $dateComponents['mday'];

}


echo build_calendar($month,$year,$day);

?>