Pin Me

How to Make a PHP Calendar

written by: nain•edited by: Linda Richter•updated: 3/30/2010

This article discusses the basics of making a PHP calendar, defining the month, years and numerical days and shows examples of relevant script that adds empty spaces to the calendar rows.

  • slide 1 of 4

    Date Functions

    One of the many interesting features of PHP are the date functions. These are of great help to anyone who wants to include a calendar on their website. Making a calendar is a relatively simple exercise, and is helped by the fact that PHP has the ability to represent days of the week in numbers. For this method Sunday is the first day – and is numbered as 0 – and Saturday the last and, of course, a number.

    In order to make a calendar we need to know the present month and year, plus the day (or its corresponding number) that was the 1st of the month, and the day that will be the last. Those four, plus the number of days in the month, are the starting point.

    Let's have a look at the first sections of script:

  • slide 2 of 4

    Giving it a name

    To start with, we name the calendar using the standard PHP scripts as follows:

    <?php

    session_start();

    ?>

    <html>

    <head>

    <title>A calendar</title>

    </head>

    <body>

    <?php

    Next we put the date and the year values as variables:

    $_SESSION['month'] = date("n");

    $_SESSION['year'] = date("Y");

    And then the variables, for ease of use, into ‘normal’ variables:

    $month = $_SESSION['month'];

    $year = $_SESSION['year'];

    The essentials of the month

    Following this, we define the first day of the month and the last day of the month, followed by the number of days in the month – as follows:

    $firstDayNumber = date("w",mktime(0,0,0,$month,1,$year));

    $lastDayNumber = date("w", mktime(0,0,0,$month+1,0,$year));

    $numberOfDays = date("t",mktime(0,0,0,$month,1,$year));

    ?>

    The rules for creating rows and columns are as generally in php, and here’s how the script looks for the first two rows:

    <table border="1" cellspacing="0" cellpadding="5">

    <tr>

    <td><b>--</b></td>

    <td align="center" colspan="5"> My Calendar</td>

    <td><b>--</b></td>

    </tr>

    <tr>

    <td><b>Su</b></td>

    <td><b>Mo</b></td>

    <td><b>Tu</b></td>

    <td><b>We</b></td>

    <td><b>Th</b></td>

    <td><b>Fr</b></td>

    <td><b>Sa</b></td>

    </tr>

    It is quite easy to follow once you know how, I’m sure you’ll agree.

  • slide 3 of 4

    Adding the blanks

    Seeing as the month did not necessarily begin on the first day of the week, here’s the way – using the numerical day values – to put in the blank spaces at the beginning:

    <tr>

    <?php

    for($i=0; $i < $firstDayNumber; $i++)

    {

    echo "<td bgcolor=silver> </td>";

    }

    ?>

    And to complete the row:

    $count = 0;

    for($j=1; $j < $numberOfDays; $j++)

    {

    $count++;

    echo "<td>$j</td>";

    if($count + $firstDayNumber ==7)

    {echo "</tr>";break;} // end of if statement}

    Now we have done one row, we can finish off the month, adding the spaces at the end thus:

    $lastnumber = (7 - $lastDayNumber)- 1;

    for($m=0; $m < $lastnumber; $m++)

    {

    echo "<td bgcolor=silver> </td>";

    }

  • slide 4 of 4

    The code above shows the fundamentals of creating rows for a PHP calendar. You must also remember that there are many different types of calendars, and many different ways of creating one for your website using PHP. Calendars can be just for a month, or a year, or for several years, and there are capabilities for doing all of these with PHP.