Publishing System Settings Logout Login Register
Creating a php based calendar
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on June 19th, 2007
8056 views
PHP Coding
Welcome to this tutorial on making a php based calendar. I've split it up into manageable chunks. You can see the type of calendar we're going to make here. This tutorial will show you how php's date functions work and you'll see "for" loops being used in a real world example.

So let's get started then��php has a many date functions, you can for example use them to find what day of the week it will be exactly 6 months from now and all sorts of similar things. Also in need of a mention is that php can represent each week day numerically....ie. Sunday = 0, Monday = 1, Tuesday = 2, right through to Saturday which is 6. (Why this is important will become clear in just a second). So�.from the outset we need to establish to five things����they are:

What the current month is,
What the current year is,
The numerical value for the weekday falling on the 1st of the month,
The numerical value for the weekday falling on the last day of the month,
The total number of days in the current month.

Let's start with the first two��.date("n") gives us the current month in numerical form... Then date("Y") gives us the current year. Now I actually want to place these two values into session variables�.(again you'll see why later)���so at the very top of our page before anything else do the following...


<?php
session_start( );
?>
 


then further down the page�..after the body do this�..


<?php
$_SESSION['month'] = date("n");
$_SESSION['year'] = date("Y");
?>
 


then I prefer to place the session variables into normal variables, so do this.


$month = $_SESSION['month'];
$year = $_SESSION['year'];


Ok, So now with the last three on the list I mentioned earlier...you can see that the date( ) and mktime( ) functions can you give you certains specifics.


// gives the numerical value for the weekday falling on the 1st of month
$firstDayNumber = date("w",mktime(0,0,0,$month,1,$year));
//gives the numerical value for the weekday falling on the last day of the month
$lastDayNumber = date("w", mktime(0,0,0,$month+1,0,$year));
//gives the total number of days in the current month
$numberOfDays = date("t",mktime(0,0,0,$month,1,$year));


Okay so that's our five things taken care of, below is a snapshot of the code you should have at this stage. If you'd like to take a break and review what we've covered so far that's fine, if you'd ready to carry on just go to page 2.


<?php
session_start();
?>

<html>
<head>
<title>My calendar</title>
</head>
<body>

<?php
// putting the date and year values into session variables
$_SESSION['month'] = date("n");
$_SESSION['year'] = date("Y");
// putting variables into normal variables
$month = $_SESSION['month'];
$year = $_SESSION['year'];
// gives the numerical value for the weekday falling on the 1st of month
$firstDayNumber = date("w",mktime(0,0,0,$month,1,$year));
// gives the numerical value for the weekday falling on the last day of the month
$lastDayNumber = date("w", mktime(0,0,0,$month+1,0,$year));
// gives the total number of days in the current month
$numberOfDays = date("t",mktime(0,0,0,$month,1,$year));
?>


The next part in making our calendar is very easy..we simply put in the static html that forms the first two rows of the table that houses our calendar�..here it is��


<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>


Next we need those blank cells that appear before the first of the month�that's why we needed the numerical value for the weekday that falls on the first of the month��remember our friend $firstDayNumber �� We just make a for loop using this number to give us the right number of blank cells....like so. (Notice I also make a new table row too)


<tr>
<?php

for($i=0; $i < $firstDayNumber; $i++)
{
echo "<td bgcolor=silver> </td>";
}

?>



Still with me so far? great! now we need to complete the table row we started. The issue here though is that at the point when the first few days of the month and the number of blank cells added together equals 7 we need to put in a </tr> thereby finishing that row. So taking our $numberOfDays variable we make a for loop churning out numbers 1 2 3 etc. Then within the for loop we use an if statement to see when our incrementing value plus $firstDayNumber equals 7, when it does we put in a </tr> and terminate the loop with a break.


$count = 0;

for($j=1; $j < $numberOfDays; $j++)
{
$count++;
echo "<td>$j</td>";
if($count + $firstDayNumber ==7)
{
echo "</tr>";
break;
} // end of if statement
}


So now what we need are rows for the remaining days of the month�.We do this in the same way as before��we use a for loop putting out a </tr> once it serves up 7 cells���however instead of starting at 1 our loop must carry on from the number the previous loop stopped at . We do that like this�


$number = 7 - $firstDayNumber;

$new_count = 1;
for($k=$number +1; $k < $numberOfDays +1 ; $k++)
{
$new_count++;
echo "<td>$k</td>";
if($new_count == 8)
{
echo "</tr>";
$new_count=0;
$new_count++;
} // end of if statement

}


Next up we need to put in the blank cells after the last day of the month so as to finish up that particular row. That's where our friend $lastDayNumber comes in. We just take that number, subract it by 7 then subtract 1 from the total. (since php starts counting from 0). Put the result into a variable and use that variable in a for loop.


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

for($m=0; $m < $lastnumber; $m++)
{
echo "<td bgcolor=silver> </td>";
}


Of course this would now finish off that html table row so we need to close with html tags...</tr> </table>....and do so outside of the php declarations. I would suggest now you take and break before going on the next part but if you're ready to carry on be my guest!.



How are we doing��..I'm really impressed you've stuck with me so far. Now we have a basic calendar in place however there remains two issues�..first we need to show what the current month and year is (on the first table row) secondly we need to enable the links at the side that allow you to flick back and forth between the months. Let's deal with the first issue for now��.I simply make an array with all the months of the year and then I access the current month...like so.


$theMonths = array("dummy","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"
,"Sep","Oct","Nov","Dec");

$theMonths[$month];


Remember $theMonths is the name I've chosen for the array��.and that $month gives us  the numerical value for the current month. Let's say for this month it's 5 so then  $theMonths[$month] allows us to access item number  5 in the $theMonths array. I'm sure you've all noticed "dummy" being the first item in the array��.this is a workaround because (once again) php starts counting from 0 ����..so dummy is actually item 0 Jan is item 1 and so on���in terms of getting this into the first table row I just do this....



<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td><b>--</b></td>
<td align="center" colspan="5"><?php echo $theMonths[$month] ." " . $year; ?></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>
<tr>



So now finally how to flick back and forth between months. This is why if you remember at the beginning I put the current month and year into session variables. The two dashes either side of the top row I'll change to less than and greater than signs. Then I'll change them to links that when clicked link back to our calendar page. Both these links respectively carry variables in a query string and our script detects whether those variables are present or not. The link on the right hand side takes you forward. If it's detected using the $_GET superglobal array our script takes the month value (which we placed in a session variable so it's remembered each time) and adds 1 to it. If the month happens to be 12 for December our script changes the month's session value to 1 for January and takes the year session value and adds 1 to it . If you click the left link the same happens but in reverse....1 is subtracted from the month Session variable. If at the same time if the month happens to be 1 for January, our script changes the month's session value to 12 for December and 1 is subracted from the year session value. Finally if no $_GET is present the script defaults to the current month and year. The actual code is here and this is placed under the body of our page. Note, this bit of code would go straight after the opening <?php tag which goes after your opening <body> tag.



if(!isset($_GET["action"]))
{
$_SESSION["month"] = date("n");
$_SESSION["year"] = date("Y");
$month = $_SESSION["month"];
$year = $_SESSION["year"];
}

if(($_GET["action"] == "fwd") and ($month !== 12))
{
$month = $month + 1;
}

if(($_GET["action"] == "fwd") and ($month == 12))
{
$month = $month - 11;
$year = $year + 1;
}

if(($_GET["action"] == "bck") and ($month !== 1))
{
$month = $month - 1;
}

if(($_GET["action"] == "bck") and ($month == 1))
{
$month = $month + 11;
$year = $year - 1;
}



Finally now here's where I take those two dashes change them into less than and greater than signs and make them links that carry the variables in.


<tr>
<td>

<!-- this link takes the calendar back one -->
<b><a href="calendar.php?action=bck"><</a></b>

</td>

<td align="center" colspan="5"><?php echo $theMonths[$month] ." " . $year; ?></td>

<td>

<!-- this link takes the calendar forward one -->
<b><a href="calendar.php?action=fwd">></a></b>

</td>
</tr>


So that's it! you've done really well sticking with me throughout this long tutorial. You can add formatting with css to your calendar, but I'll leave that up to you. You can see all the code here, and in another tutorial hopefully soon I'll show you how to take this and make it database driven. That's to say have the individual dates become links that click through to pages that list appointments you have that day.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send


This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top