I'm working with a calendar script, which more or less builds itself based on the server date. I'm having trouble placing the cell with day one on the right day of the week. The code is here:

<?php 								
$d = date();/* here is where I need help deciding what day of the week the 1st is, then placing the following inside a loop to make sure day one is on the first day of the week*/

for ($f = 0; $f == $d; $d++)
{
				echo("<td width=\"120\" height=\"100\" valign=\"top\"></td>");
}


			$i = 1;

			while($row = mysql_fetch_array($result))
			{ ?> 
<td width="120" height="100" valign="top"><p><?php echo $i; ?></p>
<p align="center"><font size="2">
<?php echo $row['event']; ?></font></p>
<?php 
$i++;
$d++;
if ( $d > 7) 
{
$d = 1;
echo "</tr><tr>";
}
	?>

Any ideas?

~JOSH

    Here's one way:

    $month = '1';
    $year  = '05';
    $day_one = strtotime($year . '-' . $month . '-1');
    $d = date('w', $day_one);
    for ($f = 0; $f < $d; $f++) { 
        echo '<td width="120" height="100" valign="top"></td>'; 
    }

      Thanks for the help. That works except for one thing, $d (which counts how many days have passed this week and starts a new row after 7) is always set one unit too high. I altered the script a bit, so it's like this:

      <?php
      $month = date(m);
      				$year  = date(y);
      				$day_one = strtotime($year . '-' . $month . '-1');
      				$d = date('w', $day_one);
      
      			for ($f = 0; $f < $d; $f++) { 
      		    echo '<td width="120" height="100" valign="top"></td>'; 
      			}				
      
      			$i = 1;
                                  $d++;
      			while($row = mysql_fetch_array($result))
      			{ ?> 
      <td width="120" height="100" valign="top"><p><?php echo $i; ?></p>
      <p align="center"><font size="2">
      <?php echo $row['event']; ?></font></p>
      <?php 
      $i++;
      $d++;
      if ( $d > 7) 
      {
      $d = 1;
      echo "</tr><tr>";
      }
      	?>
      </td> 
      <?php 
      } 
      ?> 

      After $d is used to add blank cells, I boosted it by one and that did the trick.

      Thanks,

      ~JOSH

        Write a Reply...