Creat a Calendar in PHP

in Calendar

Do you need a PHP event caledar? Do yu want a good-looking monthly occurrence chart that displays your future plan to your guests? Do you also want to a way to manage the agenda facts through a web based interface?

Some free PHP Calendar scripts listed on the page! You can check out the page to find some free php calendar scripts you want.

In this tutorial I will show you how to create a calendar script in PHP. We will create a simple but good looking - easy customizable via CSS - calendar table.

Step 1.

First of all try to collect the necessary information which are important to display the actual month and highlight the actual day. Besides this we want to display the actual month and year informations as well. To do this we need 3 special days informations:

1. The actual day
2. The first day of the actual month
3. The last day of the actual month
With the above mentioned informations we can decide what day was the first day, how long is the month and of course which is the actual day.

Step 2.

To get the information mentioned in Step 1 we will use the PHP built in function: getdate(). Without parameters this function returns the actual day informations in an array as follows:

01 Array
02 (
03 [seconds] => 40
04 [minutes] => 58
05 [hours] => 21
06 [mday] => 17
07 [wday] => 2
08 [mon]   => 6
09 [year] => 2003
10 [yday] => 167
11 [weekday] => Tuesday
12 [month] => June
13 [0] => 1055901520
14 )

To get the last day of the month with getdate we need to try to get the 0. day of the next month. So the code to get the information looks like this:

1 2 $today = getdate();
3 $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
4 $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
5 ?>

Step 3.

To display a calendar we need a table with 7 columns for the days of the week. The number of lines depending on the number of days and the first day of the month. However we need a header line with month and year information, a subheader line with the name of the days.

1 2 // Create a table with the necessary header informations
3 echo '';
4 echo '  ";
5 echo '';
6 echo '  ';
7 echo '  ';
8 ?>

Step 4.

Ok, now we have the header of the table. Let's try to fill the first row. It is not so easy as you can not just write 1 in the first cell, 2 in the second and so on. It only works if the first day of the month was Monday, but what if not? To decide this we need the wday item from the firstDay array. With this information we can fill the cells with a space if needed. The code to do this is the follows:

01 02 echo '';
03 for($i=1;$i<$firstDay['wday'];$i++){
04   echo '';
05 }
06 $actday = 0;
07 for($i=$firstDay['wday'];$i<=7;$i++){
08   $actday++;
09   echo "";
10 }
11 echo '';
12 ?>

Step 5.

As next step we need to fill to following lines. It is a bit easier, we only need to know how many full week we have and fill some table rows as follows:

01 02 $fullWeeks = floor(($lastDay['mday']-$actday)/7);
03
04 for ($i=0;$i<$fullWeeks;$i++){
05   echo '';
06   for ($j=0;$j<7;$j++){
07 $actday++;
08 echo "";
09   }
10   echo '';
11 }
12  
13 ?>

Step 6.

As semi final step we need to add the rest of the month to the last line. In this case it is quite easy:

01 02 if ($actday < $lastDay['mday']){
03   echo '';
04   for ($i=0; $i<7;$i++){
05 $actday++;
06 if ($actday <= $lastDay['mday']){
07 echo "";
08 }
09 else {
10 echo '';
11 }
12   }
13   echo '';
14 }
15 ?>

Step 7.

To make the calendar little bit nicer we will introduce some CSS design. The CSS file is very simple:

01 table {
02 width:210px;
03 border:0px solid #888; 
04 border-collapse:collapse;
05 }
06 td {
07 width:30px;
08 border-collpase:collpase;
09 border:1px solid #888;
10 text-align:right;
11 padding-right:5px;
12 }
13 .days{
14 background-color: #F1F3F5;
15 }
16 th {
17 border-collpase:collpase;
18 border:1px solid #888;
19 background-color: #E9ECEF;
20 }
21 .actday{
22 background-color: #c22;
23 font-weight:bold;
24 }

Step 8.

The complete code using the CSS is the following:

01
02
03
04
05
06
07 08 function showCalendar(){
09 // Get key day informations.
10 // We need the first and last day of the month and the actual day
11 $today = getdate();
12 $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
13 $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
14  
15 // Create a table with the necessary header informations
16 echo '

'.$today['month']." - ".$today['year']."

Mo       Tu        We        Th        Fr        Sa        Su

$actday    $actday

$actday      

';
17 echo '  ";
18 echo '';
19 echo '  ';
20 echo '  ';
21  
22 // Display the first calendar row with correct positioning
23 echo '';
24 for($i=1;$i<$firstDay['wday'];$i++){
25   echo '';
26 }
27 $actday = 0;
28 for($i=$firstDay['wday'];$i<=7;$i++){
29   $actday++;
30   if ($actday == $today['mday']) {
31 $class = ' class="actday"';
32   } else {
33 $class = '';
34   }
35   echo "$actday";
36 }
37 echo '';
38
39 //Get how many complete weeks are in the actual month
40 $fullWeeks = floor(($lastDay['mday']-$actday)/7); 
41 for ($i=0;$i<$fullWeeks;$i++){
42   echo '';
43   for ($j=0;$j<7;$j++){
44 $actday++;
45 if ($actday == $today['mday']) {
46 $class = ' class="actday"';
47 } else {
48 $class = '';
49 }
50 echo "$actday";
51   }
52   echo '';
53 }
54
55 //Now display the rest of the month
56 if ($actday < $lastDay['mday']){
57   echo '';
58   for ($i=0; $i<7;$i++){
59 $actday++;
60 if ($actday == $today['mday']) {
61 $class = ' class="actday"';
62 } else {
63 $class = '';
64 }
65
66 if ($actday <= $lastDay['mday']){
67 echo "$actday";
68 }
69 else {
70 echo '';
71 }
72   }  
73   echo '';
74 }
75 echo '

'.$today['month']." - ".$today['year']."

Mo       Tu        We        Th        Fr        Sa        Su

';
76 }
77 showCalendar();
78 ?>
79
80
Author Box
viysharing has 9 articles online

Viysharing is a PHP Programmer. Glad to share some free php scripts with peple who interested in PHP. More orginal writtern you can check out the site PHPKode.com

Add New Comment

Creat a Calendar in PHP

Log in or Create Account to post a comment.
     
*
*
Security Code: Captcha Image Change Image
This article was published on 2011/04/13