1.11M Members

how to display calendar through php in html table

 
0
 

hello,
I have written some code for it but it's not displaying date's in <td>

where is the problem can any body tell me.

<?php
	//Get year 2008
	$year = date('Y');
	echo "Year $year</br>";
	
	//get month 
	$month = date('n');
	echo "Month $month</br>";
	
	//get day 
	$day = date('j');
	echo "Day $day</br>";
	
	//get no of days in month
	$daysInMonth = date('t',mktime(0,0,0,$month,1,$year));
	echo "Days in Month $daysInMonth</br>";
	
	//get first day of the month
	$firstDay = date('w',mktime(0,0,0,$month,1,$year));
	echo "First Day of Month $firstDay</br>";
	//Calculate total no's of cell needed
	$tempDays = $firstday + $daysInMonth;
	echo "Temp days $tempDays</br>";
	//Calculate total rows needed
	$weeksInMonth = ceil($tempDays/7);
	echo "Weeks in month $weeksInMonth</br>";
	//filling the values in 2-d array
	//function
		function fillArray()
			{
				// create a 2-d array
					for($j=0;$j<$this->$weeksInMonth;$j++)
					{
						for($i=0;$i<7;$i++)
							{
							$counter++;
							$this->$week[$j][$i] = $counter;
							// offset the days
							$this->$week[$j][$i] -= $this->$firstDay;
							if (($this->$week[$j][$i] < 1) || ($this->$week[$j][$i] > $this->$daysInMonth))
								{    
								$this->$week[$j][$i] = "";
								}
							}
					}
			}
	?>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
 <head>
  <title> Calendar 2008 </title>
  <meta name="generator" content="editplus">
  <meta name="author" content="">
  <meta name="keywords" content="">
  <meta name="description" content="">
 </head>
 <body>
	<form name="f1" id="f1" method="post">
		<center>Month : <select name="cboMonth">
						<?php 
							for($m=1;$m<=12;$m++)
							{
								echo '<option value="'. $m .'">' . $m. '</option>';
							}
						?>
					</select>
		Year : 2008
		</center>
	</form>
	<table border="1" cellpadding="2" cellspacing="2" align="center" width="400">
		<th colspan='7'><?= date('M', mktime(0,0,0,$month,1,$year)).' '.$year; ?></th>
		<tr>
			<th>Sun</th>
			<th>Mon</th>
			<th>Tue</th>
			<th>Wed</th>
			<th>Thu</th>
			<th>Fri</th>
			<th>Sat</th>
		</tr>
		<?php
			foreach ($week as $key => $val)
			{ 
				echo '<tr>';
				for ($i=0;$i<7;$i++)
					{
						echo '<td align="center">'. $date .'</td>';
					}
				echo '</tr>';
			}
		?>
	</table>  
 </body>
</html>
 
0
 

I have checked your code and the last piece of php code at the end of the script should look something like below. Notice how I added $date=0 and $date+=1. That is because the date you were saying that wasn't being displayed hadn't been assigned any value at all. So try using the below code.

<?php
$date=0;
foreach ($week as $key => $val)
{
echo '<tr>';
// here you may need to add $date+=1; or $date-=1;
for ($i=0;$i<7;$i++)
{
$date+=1;
echo '<td align="center">'. $date .'</td>';
}
echo '</tr>';
}
?>

And please use code tags next time.

 
0
 

sorry.. i want to try this coding..unfortunatelay when i run this codes, nothing was display and have syntax error..i try to identified that errors but failed..

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article