Here's a very simple, but very complete, PHP
script to print out a calendar. It does one thing, but does it well
(which is how it should be). Neat bonus features include the ability to
link certain days in the calendar to a web page - useful for a weblog.
Getting Calendar Variables
PHP calendars can be very useful. You can do things as simple as
showing the date, and as complex as an online booking system. In this
tutorial we will show you how to generate a very simple PHP calendar.
Once you understand how to do this, you will be able to apply the same
concepts to more complex calendars you may need.
<?php
//This gets today's date
$date =time () ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
In the first part of our code we are setting some variables we will
need later in the script. First we find out what today's date is using
the time() function. We then use the date() function to format our date appropriately for the $day, $month, and $year
variables. Finally we use it to generate the name of the month, which
will be the title of our calendar.
Days of the Week
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
//Once we know what day of the week it falls on, we know how many blank
days occure before it. If the first day of the week is a Sunday then it
would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
Here we take a closer look at the days of the month and prepare to make
our calendar table. The first thing we do is determine what day of the
week the first of the month falls. Once we know that, we use the switch() function to determine how many blank days we need in our calendar before the first day.
Next we count the total days of the month. Now that we know how many
'blank' days we need, and how many total days are in the month we can
start to generate our calendar.
Headings and Blank Calendar Days
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td
width=42>M</td><td width=42>T</td><td
width=42>W</td><td width=42>T</td><td
width=42>F</td><td width=42>S</td></tr>";
//This counts the days in the week, up to 7
$day_count = 1;
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
The first part of this code very simply echos the table tags, the month name, and the headings for the days of the week. Then we start a while loop.
What we are doing is echoing empty table details, one for each blank
day we count down. Once the blank days are done it stops. At the same
time, our $day_count is going up by 1 each time through the loop. This
is to keep count so that we do not try to put more than seven days in a
week.
Days of the Month
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
Now we need to fill in the days of the month. We do this with another while loop, but this time we are counting up to the last day of the month. Each cycle echos a table detail with the day of the month, and it repeats until we reach the last day of the month.
Our loop also contains a conditional statement.
This checks if the days of the week have reached 7, the end of the
week. If it has, it starts a new row, and resets the counter back to 1
(the first day of the week).
Finishing the Calendar
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";
To finish our calendar we use one last while loop.
This one fills in the rest of our calendar with blank table details if
needed. Then we close our table and our script is done.
|