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>

Dani AI

Generated

You are very close. The main blockers are logic and scope, not HTML. In your snippet, $week is never populated, fillArray() is never called, and it tries to use $this->... even though there is no class. You also mix $firstDay and $firstday (different variables), and you print $date in the table even though it was never assigned. correctly noted the missing $date, but you still need to offset the first weekday and stop after the last day. , if you saw a syntax error, two common culprits here are <?= ... ?> on older PHP (use <?php echo ... ?> instead) and the invalid </br> tag (use <br />).

A minimal, self-contained approach is to compute the first day timestamp, its weekday offset, and the number of days in the month, then fill cells by index. No classes, no hidden state:

<?php
$year  = 2008; // or (int)date('Y')
$month = isset($_POST['cboMonth']) ? max(1, min(12, (int)$_POST['cboMonth'])) : (int)date('n');

$first  = mktime(0, 0, 0, $month, 1, $year);
$days   = (int)date('t', $first);
$offset = (int)date('w', $first); // 0=Sun..6=Sat

echo '<table border="1" cellpadding="2" cellspacing="2" align="center" width="400">';
echo '<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>';

$cell = 0;
for ($r = 0; $r < 6; $r++) {
    echo '<tr>';
    for ($c = 0; $c < 7; $c++, $cell++) {
        $d = $cell - $offset + 1;
        echo '<td align="center">' . ($d >= 1 && $d <= $days ? $d : '&nbsp;') . '</td>';
    }
    echo '</tr>';
    if ($cell - $offset >= $days) break; // stop once the month ends
}
echo '</table>';

Tips:

  • If you want Monday as the first column, use date('N', $first) and convert to a 0..6 offset with (($n + 6) % 7).
  • When you add a month dropdown, remember to selected the posted month and sanitize with (int).

Recommended Answers

All 2 Replies

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.

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.