Home > Articles > Programming > PHP

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Conditional Statements

The calendar itself is almost finished, but we still need to make sure that each week appears on its own line. We can do that with an if-then-else statement, otherwise known as a conditional statement.

In this case, we want to check the day of the week for each day, and start a new row if the day is Sunday (see Listing 8).

Listing 8—Creating One Row Per Week

...
<?php

  //Align the first day
  $daynum = jddayofweek(gregoriantojd($month, 1, $year), 0);
  for ($blankdays = 0; $blankdays < $daynum; $blankdays++){
    echo("<td>&nbsp;</td>");
  }

  //Display the rest of the month
  for ($i = 1; $i <= $numberofdays; $i++) {

    //If it's a Sunday, start a new row; otherwise, just display
    $thisdayofweek = jddayofweek(gregoriantojd($month, $i, $year), 0);
    if ( $thisdayofweek == 0) {
      echo("</tr><tr><td>$i</td>");
    } else {
      echo("<td>$i</td>");
    }
  }

?>
    </tr>
  </table>
</body>
</html>

Notice the form that the if-then-else statement takes:

if ( expression ) {
  // Do this if expression evaluates to true
} else {
  // Do this if expression evaluates to false
}

Notice also that the expression we're evaluating has a slightly different form, using == instead of =. The reason is that == denotes a comparison; we want to know if the variable $thisdayofweek is equal to 0. The single equals sign (=) is an assignment operator. If we'd said

$thisdayofweek = 0

we'd be assigning a value of 0 to the $thisdayofweek variable. There wouldn't be a problem with this assignment, so the expression would always be true, which isn't what we want.

With the comparison operator, the end result is that each time a Sunday is displayed, the previous row ends and a new one begins, as shown in Figure 7.

Figure 7Figure 7 Displaying one row per week.

We have one last task before the calendar itself is complete. The unused days of the week after the end of the month need to be accounted for, just as we accounted for those that fell before the beginning of the month. Listing 9 shows how it works.

Listing 9—Completing the Month

...
  //Display the rest of the month
  for ($i = 1; $i <= $numberofdays; $i++) {

    //If it's a Sunday, start a new row; otherwise, just display
    $thisdayofweek = jddayofweek(gregoriantojd($month, $i, $year), 0);
    if ( $thisdayofweek == 0) {
      echo("</tr><tr><td>$i</td>");
    } else {
      echo("<td>$i</td>");
    }
  }

  //Complete the month
  $julianday = gregoriantojd($month, $numberofdays, $year);
  $daynum = jddayofweek($julianday, 0);
  for ($blankdays = $daynum+1; $blankdays < 7; $blankdays++){
    echo("<td>&nbsp;</td>");
  }

?>
    </tr>
  </table>
</body>
</html>

Here we're starting with the day after the last day of the month and creating empty table-data (<td></td>) cells for the rest of the week.

Now the calendar's complete, but we still need a way to choose which month to display.

  • + Share This
  • 🔖 Save To Your Account