Feature
Post

Category
Code


Create a Simple Calendar Using PHP

Here’s a simple, useful calendar script. This is a great way to check on days of the week for birthdays, holidays and other important occasions. You can also use it in conjunction with a data file (e.g. .csv, .xml) or database table (e.g. MySQL) as a tool to track appointments and special events.

This particular script makes many uses of the PHP date formatting functions. For more information on how PHP date formating works, see the PHP manual (http://us3.php.net/date).

First off, check to see if the user has sent previous values for the month and year via the form or the query string.
If this is the initial load of the form, then the system will pull the current month and year.
The “isset” function checks for the existence of the $_GET (query string) variables.

<!--//if values are not set for $Month and $Year, get current date values
if1 && (!isset($_GET["Year"]))) {
 $Month = date("m");
 $Year = date("Y");
}
else{
 $Month = $_GET["Month"];
 $Year = $_GET["Year"];
}

Now, we use the month and year values to get the Unix timestamp:

//Get viewed month
$Timestamp = mktime(0,0,0,$Month,1,$Year);

We can use the date formatting function to get the full month name (e.g. January, February, etc.):

$MonthName = date("F", $Timestamp);

We use the “echo” function to print the start of the table to the screen:

//Make calendar table
echo("Calendar Table for $MonthName, $Year
");
echo(
<table border="0" cellspacing="0" cellpadding="3" align="center">");
echo("
<tbody>
<tr bgcolor="#0000ff">");

We create the array for the days of the week and loop through the array elements to fill the table headers:

//loop through days
$daysOfWeek = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
foreach ($daysOfWeek as $value) {
echo("
<td align="center"><strong>$value</strong></td>
");
}
echo("</tr>
");

Here is another date formatting function that we can use to get the day of the week for the first day of the month. Remember, this is a zero-indexed array. Thus, 0=Sunday, 1=Monday, etc.

//get day of week at start of month
$MonthStart = date("w", $Timestamp);
if ($MonthStart == 0) { // if the month starts on a Sunday
 $MonthStart = 7;
}

Now we get the last day of the month by “0th” day of the next month:

//get last day of month
$LastDay = date("d", mktime(0,0,0,$Month+1, 0, $Year);

Another option is to find the number of days in the current month. Using this option will affect the calculations further down the routine. If you like, you can use this option and adapt the rest of the routine to this new setting.

//$LastDay = date("t", $Month);

Get the number of “blank” entries at the start of the month by using the day of the week that starts the month.

//get start date
$StartDate = -$MonthStart;

Now we can start filling the table cells for the calendar.

First, the we set up six rows for up to six weeks:

for ($k=1;$k&lt;=6,$k++){  //print six rows for six possible weeks
echo("<tr>");

Then, we set up the columns for the seven days of the week:

for ($j=1;$j&lt;=7;$j++){ //seven columns per row

The system will add 1 to the $StartDate variable for each day in the week. From there, we test the value of the variable. If the $StartDate variable is either before the start of the month or after the end of the month, the calendar shows a blank space. If the $StartDate variable is within the number of days of the month, the calendar prints the date.

 $StartDate++;
 if(($StartDate  $LastDay) { //blank calendar space
  echo("
<td> </td>
");
  }
 elseif2 { //date goes here
  echo("
<td>$StartDate</td>
");  
  }
 }

At the end of the week, we end the table row and start a new week.

 echo("</tr>
"); 
} //End Table Row

After we’ve finished all six weeks, we end the table.

 echo("</tbody></table>
");

That takes care of the current month. Now what do we do if we want to see another month?

Here’s how we create a form that allows a user to select a different month:

 //create select form

 echo("
<div><form action="\calendar.php\" accept-charset="UNKNOWN" enctype="application/x-www-form-urlencoded" method="\GET\">");
 echo("Select a new month to view:
");
</form></div>
<form action="\calendar.php\" accept-charset="UNKNOWN" enctype="application/x-www-form-urlencoded" method="\GET\">

Instead of listing the months individually, we can use the date formatting function to create a dynamic loop and display the month names:

 echo("");   for($m=1;$m&lt;=12;$m++){  echo("" .date("F", mktime(0,0,0,$m,1,$Year)) . "");  }  echo("");

We can do the same for the year (2008-2020):

echo("");  for($y=2008;$y&lt;2020;$y++){  echo("$y"); } echo("");

Now we finish the form with the submit button:

echo("");
echo("");

</form>

Again, this is a simple yet functional calendar script. If you’d like to see how to add other features to this calendar, such as flat file or database support, please let us know.

  1. !isset($_GET["Month"] []
  2. $StartDate &lt;=1 &amp;&amp; ($StartDate &lt;= $LastDay []

  1. By Michael Kane posted on January 8, 2009 at 6:39 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    First let me say that I am new to PHP. I cannot get past line two without an error!
    1<?php
    2//if values are not set for $Month and $Year, get current date values
    3if ((!isset($_GET["Month"]))&& (!isset($_GET["Year"])))
    4 {
    5 $Month = date(“m”);
    6 $Year = date(“Y”);
    7 }
    8 else{
    9  $Month = $_GET["Month"];
    10  $Year = $_GET["Year"];
    11 }

    This is the error message
    Parse error: syntax error, unexpected ‘;’ in C:\wamp\www\LAFestUpload\calendarTest.php on line 3

    Any help would be appreciated.
    Thank you.
    Michael

  2. By piyush posted on January 28, 2009 at 5:45 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    thanks a lot for providing such a helpful script on the net.
    ……………….Thanks once again…………….

  3. By Json posted on February 15, 2009 at 10:14 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Neat idea nothing to see…
    Demo, Preview or anything to see what it looks like?
    Regards,
    Cheers

  4. By KC posted on March 12, 2009 at 7:01 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Great code… but alot of mistakes,
    im going to try and upload a revised version.

    <?php

    echo “”;
    echo “Calendar Table for $MonthName, $Year”;
    echo “”;

    $daysOfWeek = array(“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”);

    foreach ($daysOfWeek as $value) {
    echo “$value“;
    }
    echo “”;

    $MonthStart = date(“w”, $Timestamp);
    if ($MonthStart == 0) {
    // if the month starts on a Sunday
    $MonthStart = 7;
    }

    $LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year));
    $StartDate = -$MonthStart;

    for ($k=1;$k<=6;$k++){ //print six rows for six possible weeks
    echo”";
    for ($j=1;$j<=7;$j++){ //seven columns per row
    $StartDate++;
    if($StartDate 0) {
    echo”$StartDate \n”;
    } else {
    echo” \n”;
    }
    } elseif (($StartDate = $LastDay)) { //date goes here
    if($StartDate >= 0) {
    echo”$StartDate \n”;
    }
    }
    }
    echo”";
    } //End Table Row

    echo “”;
    ?>

    <?php
    echo “”;
    for($m=1;$m<=12;$m++){
    $selected = “”;
    $longDate = date(“F”, mktime(0,0,0,$m,1,$Year));
    if ($Month==$m){
    $selected = “selected “;
    }
    echo “$longDate \n”;
    }
    echo “”;
    echo “”;
    for($y=2007;$y<=2020;$y++){
    $selected = “”;
    $longDate = date(“Y”, mktime(0,0,0,1,1,$y));
    if ($Year==$y){
    $selected = “selected \n”;
    }
    echo “$longDate \n”;
    }
    echo “”;
    ?>

  5. By KC posted on March 12, 2009 at 7:06 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    WOW, that got eaten alive.. so much for trying.
    for a working version go here:
    http://www.dyertech.ca/current/cal.php

  6. By Tim Hall posted on March 18, 2009 at 4:10 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Nice article,
    I had been pondering this but you saved me the brain power. A special thanks to KC for the example and working code.

    Nice work

  7. By Shaaa posted on April 16, 2009 at 10:18 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Nice article…
    But there is an issue… in each month it shows as 1 day less than total number of days.

  8. By Shaaa posted on April 16, 2009 at 10:27 am
    Want an avatar? Get a gravatar! • You can link to this comment

    i have changed the following line,

    $LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year));

    into this,

    $LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year))+1;

    now it works fine

  9. By BUN Seak Leang posted on May 11, 2009 at 8:59 am
    Want an avatar? Get a gravatar! • You can link to this comment

    PHP is very new to me. Recently, my teacher give me a home work “build a calender using internet programming language”. I believe that this sample will be very useful to my work.

  10. By sedkonz posted on July 19, 2009 at 2:35 am
    Want an avatar? Get a gravatar! • You can link to this comment

    hi , that s great calendar! but can we change the days number into my own value.
    maybe someone colud give some example…-php newbie

  11. TrackbackCreate a Simple Calendar Using PHP | Devlounge