Calendar - Whole year view

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Calendar - Whole year view

Post by JayBird »

I want to be able to generate a whole year view calendar that looks like the following.

Code: Select all

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
      | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| Mon |  1  |  1  |  1  |  1  |  1  |  1  |  1  |  1  |  1  |  1  |  1  |  1  |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| Tue |  2  |  2  |  2  |  2  |  2  |  2  |  2  |  2  |  2  |  2  |  2  |  2  |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
etc etc.....


Obviously the dates will need to be offest to suit the day of the month, but i think you get the idea.

Has anyone done a script like this i can "borrow", or show me how i can achieve it?

Thanks

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

bumpety bump!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Loop this, add you insert statements after you tweaked it. (Not tested, couldnt test it tho)

Code: Select all

<pre><?php
// your loop should be using the second 1 (to 365).
// the 32th January, should be parsed as 1st Feb...
echo date("D M d", mktime(0,0,0,1,1,2003))."\n"; // first
echo date("D M d", mktime(0,0,0,1,32,2003))."\n"; // checking what 32 means
echo date("D M d", mktime(0,0,0,1,365,2003))."\n"; // last
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do you want it to be like

Code: Select all

jan feb mar
mon 1
tue 2       1
wed 3       2
thu 4   1   3
or

Code: Select all

jan feb mar
tue 1
wed 2
thu 3
fri 4   1   1
?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

the first one
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this is what i have got so far

Code: Select all

<?php

echo "<table border="1">";
echo "<tr>";

$dayArray = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");

foreach ($dayArray as $checkDay) {
$month = 1;
$day = 1;
	while ($month <= 12) {
		if (date("D", mktime(0,0,0,$month,1,2003)) == $checkDay) {

		echo "<td>";
		echo date("M-d-D", mktime(0,0,0,$month,1,2003));
		echo "</td>";

		} else {
			echo "<td>&nbsp;</td>";
		}
		if ($month == 12) {
			echo "</tr>";
			echo "<tr>";
		}
		
	$month++;
	$day++;	

	}
}

echo "</tr>";
echo "</table>";
?>
still a lot to do though
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

haven't found a class that will do. So this is a quickhack that seems to work (somehow)

Code: Select all

<html>
	<body>
<?php

class SimpleCalendar
{
	var $monthsInfo = array();
	var $dayNames = array();
	var $monthNames = array();

	function SimpleCalendar()
	{
		$this->dayNames = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
		$this->monthNames = array('jan', 'feb', 'mar', 'apr', 'mai', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dez');
	}
	
	function create($year)
	{
		
		$this->monthsInfo = array(
			$this->monthNames[0]=>array('days'=>31),
			$this->monthNames[1]=>array('days'=>($this->isLeap($year))?29:28),
			$this->monthNames[2]=>array('days'=>31),
			$this->monthNames[3]=>array('days'=>30),	
			$this->monthNames[4]=>array('days'=>31),	
			$this->monthNames[5]=>array('days'=>30),	
			$this->monthNames[6]=>array('days'=>31),	
			$this->monthNames[7]=>array('days'=>31),	
			$this->monthNames[8]=>array('days'=>30),	
			$this->monthNames[9]=>array('days'=>31),	
			$this->monthNames[10]=>array('days'=>30),	
			$this->monthNames[11]=>array('days'=>31)
		);
		
		$offset = getdate(mktime(12,0,0,1,1, $year));
		$offset = $offset['wday'];

		foreach($this->monthsInfo as $abbrev=>$month)
		{
			$this->monthsInfo[$abbrev]['firstDayOfWeek'] = $offset;	
			$offset += $this->monthsInfo[$abbrev]['days'];
			$offset %= 7;
		}
	}
	
	function isLeap($year)
	{
		if (!($year % 4 == 0))
    	return false;
  	else if (($year % 100 == 0) && (!($year % 400 == 0)))
	    return false;
  	else
    	return true;
	}

	function staticView($calendarSink)
	{
		for($week = 0; $week != 6; $week++)
		{
			$calendarSink->weekBegin($week);
			foreach($this->dayNames as $dayOfWeek=>$nameOfDay)
			{
				$calendarSink->dayRowBegin($dayOfWeek, $nameOfDay);
				$pos = $week*7 + $dayOfWeek+1;
				foreach($this->monthsInfo as $monthName=>$month)
				{
					$dayOfMonth = $pos-$month['firstDayOfWeek'];
					if ($dayOfMonth > 0 && $dayOfMonth <= $month['days'])
						$calendarSink->cellDay($monthName, $dayOfMonth);
					else
						$calendarSink->cellEmpty($monthName, $dayOfMonth);
				}
				$calendarSink->dayRowEnd($dayOfWeek, $nameOfDay);
			}
			$calendarSink->weekEnd($week);
		}
	}
}

class MyCalendarSink
{
	
	function weekBegin($week)
	{
		echo '<!-- begin week #', $week, '-->';
	}
	
	function weekEnd($week)
	{
		echo '<!-- end week #', $week, '-->';
	}
	
	function dayRowBegin($dayOfWeek, $nameOfDay)
	{
		echo '<tr><td>', $nameOfDay, '</td>';
	}
	
	function dayRowEnd($dayOfWeek, $nameOfDay)
	{
		echo '</tr>';
	}
	
	function cellDay($monthName, $dayOfMonth)
	{
		echo '<td>', $dayOfMonth, '</td>';
	}
	
	function cellEmpty($monthName, $dayOfMonth)
	{
		echo '<td>&nbsp;</td>';
	}
}

$mySink = new MyCalendarSink();	
$calendar = new SimpleCalendar();
for($year=1995; $year!=2015; $year++) {
	$calendar->create($year);
?>
		<table border="1">
			<tr>
				<th colspan="13"><?php echo $year; ?></th>
			</tr>
			<tr>
				<td>&nbsp;</td>
<?php	foreach($calendar->monthNames as $name) { ?>
				<td><?php echo $name; ?></td>
<?php } ?>				
			</tr>
<?php


$calendar->staticView($mySink);
?>
		</table>
		<hr />
<?php
}
?> 
	</body>
</html>
I only took a look at 2003 jan-aug, so you'd better test it carefully ;)
Might also be someone has a good link or knows how to do it with less code
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yup ,that seems to work, bit compicated though. Haven't got a clue how the hell that works :/
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

okay, i have finally got it working with my own code. Unfortunatelt, it is very long winded, wondering if anyone can see anywhere where i can optimize the code?

here it is

Code: Select all

<pre><?php

echo "<table width="100%" border="1">";
echo "<tr>";

$dayArray = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");

$dayMonths = 1;
$day = 0;

$january = 1;
$february = 1;
$march = 1;
$april = 1;
$may = 1;
$june = 1;
$july = 1;
$august = 1;
$september = 1;
$october = 1;
$november = 1;
$december = 1;

$month = 1;

echo "<td>&nbsp;</td>";

while ($month <= 12) {
	echo "<td>";
	echo date("M", mktime(0,0,0,$month,1,2003));
	echo "</td>";
	$month++;
}
echo "</tr><tr>";

while ($dayMonths <= 37) { // The maximum number of cells any month will need is 37

	if ($day == 0 || $day > 6) { // If day = sunday, start back at monday
		$day = 0;
		$checkDay = $dayArray[$day];
	} else {
		$checkDay = $dayArray[$day];
	}


	echo "<td>$checkDay</td>";

			// January check
			
			// If the current day number is equal to the name of the day and we are still in the same month, output the dat
			if (date("D", mktime(0,0,0,1,$january,2003)) == $checkDay && date("M", mktime(0,0,0,1,$january,2003)) == "Jan") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,1,$january,2003));
			echo "</td>";
			
			$january++;

			} else { // Otherwise, print a blank cell
				echo "<td>&nbsp;</td>";
			}
			
			// February check
			if (date("D", mktime(0,0,0,2,$february,2003)) == $checkDay && date("M", mktime(0,0,0,2,$february,2003)) == "Feb") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,2,$february,2003));
			echo "</td>";
			
			$february++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// March check
			if (date("D", mktime(0,0,0,3,$march,2003)) == $checkDay && date("M", mktime(0,0,0,3,$march,2003)) == "Mar") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,3,$march,2003));
			echo "</td>";
			
			$march++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// April check
			if (date("D", mktime(0,0,0,4,$april,2003)) == $checkDay && date("M", mktime(0,0,0,4,$april,2003)) == "Apr") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,4,$april,2003));
			echo "</td>";
			
			$april++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// May check
			if (date("D", mktime(0,0,0,5,$may,2003)) == $checkDay && date("M", mktime(0,0,0,5,$may,2003)) == "May") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,5,$may,2003));
			echo "</td>";
			
			$may++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// June check
			if (date("D", mktime(0,0,0,6,$june,2003)) == $checkDay && date("M", mktime(0,0,0,6,$june,2003)) == "Jun") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,6,$june,2003));
			echo "</td>";
			
			$june++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// July check
			if (date("D", mktime(0,0,0,7,$july,2003)) == $checkDay && date("M", mktime(0,0,0,7,$july,2003)) == "Jul") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,7,$july,2003));
			echo "</td>";
			
			$july++;

			} else {
				echo "<td>&nbsp;</td>";
			}			
			
			// August check
			if (date("D", mktime(0,0,0,8,$august,2003)) == $checkDay && date("M", mktime(0,0,0,8,$august,2003)) == "Aug") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,8,$august,2003));
			echo "</td>";
			
			$august++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// September check
			if (date("D", mktime(0,0,0,9,$september,2003)) == $checkDay && date("M", mktime(0,0,0,9,$september,2003)) == "Sep") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,9,$september,2003));
			echo "</td>";
			
			$september++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// October check
			if (date("D", mktime(0,0,0,10,$october,2003)) == $checkDay && date("M", mktime(0,0,0,10,$october,2003)) == "Oct") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,10,$october,2003));
			echo "</td>";
			
			$october++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// November check
			if (date("D", mktime(0,0,0,11,$november,2003)) == $checkDay && date("M", mktime(0,0,0,11,$november,2003)) == "Nov") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,11,$november,2003));
			echo "</td>";
			
			$november++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
			// December check
			if (date("D", mktime(0,0,0,12,$december,2003)) == $checkDay && date("M", mktime(0,0,0,12,$december,2003)) == "Dec") {

			echo "<td>";
			echo date("M-d-D", mktime(0,0,0,12,$december,2003));
			echo "</td>";
			
			$december++;

			} else {
				echo "<td>&nbsp;</td>";
			}
			
echo "</tr>";
echo "<tr>";
			
$day++;
$dayMonths++;
}

echo "</tr>";
echo "</table>";

?>
Mark
Post Reply