Calendar

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
trock74
Forum Newbie
Posts: 6
Joined: Mon Jun 12, 2006 10:05 pm

Calendar

Post by trock74 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm trying to create a calender, but I can not line my dates up on the same row. It list the dates on the correct day, but it puts is date in a new table row which I'm not trying to do. Here is my code listed below and the out put. What I'm I doing wrong, any suggestion?

Code:

Code: Select all

<html>
<title>Test Calendar</title>
<head></head>
<body>
<table border="0" cellpadding="2" cellspacing="2" bgcolor="#ffffff">
<tr valign="top">
<td align=center>Sun</td>
<td align=center>Mon</td>
<td align=center>Tue</td>
<td align=center>Wed</td>
<td align=center>Thu</td>
<td align=center>Fri</td>
<td align=center>Sat</td>
</tr>
<?php
// set numberic representation of a month
$nmon = date("n");
// set number of days in the given month
$ndays = date("t");
// set full numeric representation of year, 4 digits
$nyear = date("Y");
echo "<tr>\n";
for ($i = 1; $i <= $ndays; $i++) { // loop the number of days in the month
        for ($j = 0; $j <= 6; $j++) { // loop to find which day is the 1st on
                $dno = date("w", mktime(0, 0, 0, $nmon, $i, $nyear)); // capture what day is the 1st on
                if ($dno == $j) { // if
                        echo "<td align=center>$i</td>\n";
                }
                else {
                echo "<td>&nbsp;</td>\n";
                }
        }
        echo "</tr>\n";
}
?>
</body>
</html>
Output:

Code: Select all

Sun Mon Tue Wed Thu Fri Sat 
                              1     
                                    2   
                                         3 
4             
      5           
              6         
                      7       
                              8     
                                    9   
                                         10 
11             
      12           
              13         
                      14       
                              15     
                                    16   
                                         17 
18             
      19           
              20         
                      21       
                              22     
                                    23   
                                         24 
25             
      26           
              27         
                      28       
                              29     
                                    30

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Your opening <tr> tag is outside of the loop your closing tag is inside the loop:

Code: Select all

echo "<tr>\n";
for ($i = 1; $i <= $ndays; $i++) { // loop the number of days in the month
        for ($j = 0; $j <= 6; $j++) { // loop to find which day is the 1st on
        }
        echo "</tr>\n"; 
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This works as expected

Code: Select all

<html>
<title>Test Calendar</title>
<head></head>
<body>
<table border="0" cellpadding="2" cellspacing="2" bgcolor="#ffffff">
<tr valign="top">
<td align=center>Sun</td>
<td align=center>Mon</td>
<td align=center>Tue</td>
<td align=center>Wed</td>
<td align=center>Thu</td>
<td align=center>Fri</td>
<td align=center>Sat</td>
</tr>
<?php
// set numberic representation of a month
$nmon = date("n");
// set number of days in the given month
$ndays = date("t");
// set full numeric representation of year, 4 digits
$nyear = date("Y");

//	Reset the start to Sunday the week of the first.
$i = 1 - date('w', mktime(0, 0, 0, $nmon, 1, $nyear));
while($i <= $ndays) { // loop the number of days in the month
	echo "<tr>\n";
	for ($j = 0; $j <= 6; $j++, $i++) { // loop to find which day is the 1st on
		if ($i >= 1 and $i <= $ndays) { // if the day number is in the valid range
			echo "<td align=\"center\">$i</td>\n";
		}
		else {
			echo "<td>&nbsp;</td>\n";
		}
	}
	echo "</tr>\n";
}

?>
</body>
</html>
trock74
Forum Newbie
Posts: 6
Joined: Mon Jun 12, 2006 10:05 pm

Calendar

Post by trock74 »

Thanks guys, it works like a charm.
Post Reply