Using Functions
To display dates in their proper positions on the calendar, we need to be able to determine the day of the week on which each date falls. We can do that by using functions. A function is a call that (in most cases) returns a value. For example, PHP recognizes several different calendars, including the Gregorian calendar and Julian dates. The Gregorian calendar is the familiar January-to-December system; the Julian calendar is a similar calendar that preceded the Gregorian version. Many date functions are measured in terms of the "Julian Day," or number of days since November 25, 4714 BC.
For example, to get the Julian Day for November 15, 2002, you could use the gregoriantojd() function:
$julianday = gregoriantojd(11, 15, 2002); echo ($julianday);
The value returned by the function is assigned to the $julianday variable, so the response would be 2452594.
You can also nest functions. For example, we need to find the day of the week for the first day of the month. We can do that using the $jddayofweek() function, but it takes the Julian Day as an argument. We could do this in two steps:
$julianday = gregoriantojd(11, 1, 2002); $daynum = jddayofweek($julianday, 0);
Or we can nest the function calls:
$daynum = jddayofweek(gregoriantojd(11, 1, 2002), 0);
The value returned by the gregoriantojd() function is used as input for the jddayofweek() function.
In this case, we're providing not just the Julian Day, but also the "mode" in which we want the day of the week return. For example, a mode value of 0 returns an integer from 0 (Sunday) to 6 (Saturday). A mode value of 1 returns the full name of the day, and 2 returns the three-letter abbreviation (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
We can use this information to place the first day of the month on the proper day of the week. Suppose the first is a Wednesday. Because Wednesday has a value of 3, we need to account for the three days in the week (with values of 0, 1, and 2) prior to Wednesday, by adding three "blank" days before Wednesday. In Listing 7, we'll arbitrarily choose a month and properly align the first week:
Listing 7Aligning the First Week Properly
<html> <head><title>Event Calendar</title></head> <body> <?php //Initialize the variables $numberofdays = 31; $month = 1; $year = 2003; $daysoftheweek = array("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"); ?> <h1>This Month's Events</h1> <table border="1"> <tr> <?php //Set headings foreach ($daysoftheweek as $thisday) { echo("<th>$thisday</th>"); } ?> </tr> <tr> <?php //Align the first day $daynum = jddayofweek(gregoriantojd($month, 1, $year), 0); for ($blankdays = 0; $blankdays < $daynum; $blankdays++){ echo("<td> </td>"); } //Display the rest of the month for ($i = 1; $i <= $numberofdays; $i++) { echo("<td>$i</td>"); } ?> </tr> </table> </body> </html>
Here's how it works:
Arbitrarily choose a month and year for this calendar. (In the section "Getting Form Values," we'll pull this information from a form on the page.)
Get the proper day of the week for the first day of the month.
Execute a loop that creates an empty table-data (<td></td>) cell for each day of the week that precedes the first of the month.
NOTE
Now that the page is getting a bit more complex, I've added comments, preceded by two slashes (//).
Figure 6 shows the results. While the first day of the month is taken care of, we still need to make each week appear on its own line. We can do that with conditional statements.
Figure 6 Setting the first day properly.