26337 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: Jun 30
June Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Nokia Lumia 925:
The best Windows
Phone yet
Brocade: People try
to put us down,
talkin" "bout my
(Fibre Chan)
generation
Hey, wannabe
Murdochs, yet more
chances to run your
own telly station
Wake up, Uncle
Fester! Huawei?s
nattering about
BUYING Nokia
Home Office boffins
slip out
passport-scanning
Android app
Boffins light way
to photonic
computing with 1TB
DVD tech
Welcome our GIANT
TITANIUM INSECT
OVERLORDS
Hi? Vladimir? It"s
Obama. The hackers
ARE BACK. Hello?
Are you still
there?
Intel joins
Alliance for
Wireless Power
LinkedIn DNS
hijacked, site
offline
Slashdot
Why Your Sysadmin
Hates You
21 Financial Sites
Found To Store
Sensitive Data In
Browser Disk Cache
US and Russia Set
Up Cyber Cold War
Hotline
Amazon Vows To
Fight Government
Requests For Data
2 Men Accused of
Trying To Make
X-Ray Weapon
Monsanto Executive
Wins World Food
Prize
Microsoft Launches
$100k Bug Bounty
Program
The Plight of Star
Wars Droids
Java API and
Microsoft"s
.NET API: a
Comparison
MakerBot Merging
With Stratasys
Article viewer

How to create your own PHP Calendar



Written by:auzzie
Published by:bb
Published on:2011-04-20 14:00:29
Topic:PHP
Search OSI about PHP.More articles by auzzie.
 viewed 9473 times send this article printer friendly

Digg this!
    Rate this article :
Online calendars can be useful and can vary in complexity from a simple calendar to a full blown online booking system. In this tutorial i am going to cover the former of the two.
First thing that needs to be done is to work out what information we will need to collect and display...

Online calendars can be useful and can vary in complexity from a simple calendar to a full blown online booking system. In this tutorial i am going to cover the former of the two.
First thing that needs to be done is to work out what information we will need to collect and display. For this simpler system you would only need three pieces of information. 1) What day it is today 2) The first day of the month 3) The last day of the month.
The function that I use for retrieving all of the needed date information is PHP’s built in getdate() function. This function only has one parameter which is otional and that is the timestamp of the date you want. If you don’t use a parameter like we will in this tutorial then you will have the following array available to you.
Array
(
   [seconds] => 40
   [minutes] => 58
   [hours] => 21
   [mday] => 17
   [wday] => 2
   [mon] => 6
   [year] => 2003
   [yday] => 167
   [weekday] => Tuesday
   [month] => June
   [0] => 1055901520
)


Another very useful function that we will use is mktime() which gets the Unix timestamp for a date based on the parameters passed to it. We are going to use this function along with getdate() to work out what day is the first and last day of the month. The following example is a way in which we can do this by using the next months “zero day”.
<?php
$now = getdate();
$monthStart = getdate(mktime(0, 0, 0, $now[‘mon’], 1, $now[‘year’]));
$monthEnd = getdate(mktime(0, 0, 0, $now[‘mon’] + 1, 0, $now[‘year’]));
?>


Now that we have all of this needed information it is time to actually create the calendar, now i know you a thinking oh that is easy we need seven columns and four rows that count from one up to thirty or thirty-one... If that is you then you are wrong.
The reason you are wrong is that over the space of a year there is fifty-two weeks, and not every month is spread over four of them, this is because not every month starts on the same day. The information that we know is consistent regardless of the month is that there will be seven columns and that we will need a header line for the month and year information, as well as needing another line to hold the name of each day of the week. Below is an example of the html.
<html>
     <head>
        <title>
            My Calendar viewing <?php echo($now[‘month’] . ‘ ‘ . $now[‘year’]); ?>
        </title>
    </head>
    <body>
        <table>
            <tr>
                <th colspan=”7”>
                    <?php echo($now[‘month’] . ‘ ‘ . $now[‘year’]); ?>
                </th>
            </tr>
            <tr>
                <td>
                    Monday
                </td>
                <td>
                    Tuesday
                </td>
                <td>
                   Wednesday
                </td>
                <td>
                    Thursday
                </td>
                <td>
                    Friday
                </td>
                <td>
                    Saturday
                </td>
                <td>
                    Sunday
                </td>
            </tr>


Now that we have created the top of the calendar table, it is time to start creating each of the weeks needed for the current month. As we said earlier not all months start on a Monday so we will need a couple of lines of code to find out what day is the first day of the month, then populate that cell with the number 1.
Let’s make things a little easier by starting off creating the first week. We are going to do this is by using the array we stored in $monthStart. This array is going to follow the same format as our first example. We will need just the value in the wday parameter. The following example will show you the best way in which to use that information to present the outcome that we need.
            <tr>
<?php
for($counter = 1; $counter < $monthStart['wday']; $counter++)
{
?>
                <td>
                      
                </td>
<?php
}

$today = 0;

for($counter = $monthStart['wday']; $counter <= 7; $counter++)
{
    $today ++;
?>
                <td>
                    $today
                </td>
<?php
}
?>
            </tr>

As you are able to see from the code example, we use two for loops, one to create blank squares that precede the first day of the month, and the other to complete the full week for us. All that is left is the easier task of completing the month. So all we need to do is work out how many full weeks we have gone through as well as actually filling out the table.
<?php
    $totalWeeks = floor(($monthEnd['mday'] - $today) / 7);
    
    for($counter = 0; $counter < $totalWeeks; $counter++)
   {
        echo '<tr>';
        
        for($i = 0; $i < 7; $i++)
       {
            $today ++;
            echo "<td>$today</td>";
       }
        echo '</tr>';
    }

   if($today < $monthEnd['mday'])
  {
        echo '<tr>';
        
        for($i = 0; $i < 7; $i++)
       {
            $today++;

            if($today <= $monthEnd['mday'])
            {
                echo "<td>$today</td>";
            } else {
                echo '<td> </td>';
            }
        }
        
        echo '</tr>';
    }
?>

There you have it, you have finally completed the entire month for the calendar. Some ideas for expanding the calendar would be to possibly add a link either side of the month that could change the month to either one month in the past or one month forward. Another would be to hook it up to a database so you could display events for the days displayed. Finally here is a full listing of the code.
<?php
$now = getdate();
$monthStart = getdate(mktime(0, 0, 0, $now[‘mon’], 1, $now[‘year’]));
$monthEnd = getdate(mktime(0, 0, 0, $now[‘mon’] + 1, 0, $now[‘year’]));
?>
<html>
     <head>
        <title>
            My Calendar viewing <?php echo($now[‘month’] . ‘ ‘ . $now[‘year’]); ?>
        </title>
    </head>
    <body>
        <table>
            <tr>
                <th colspan=”7”>
                    <?php echo($now[‘month’] . ‘ ‘ . $now[‘year’]); ?>
                </th>
            </tr>
            <tr>
                <td>
                    Monday
                </td>
                <td>
                    Tuesday
                </td>
                <td>
                   Wednesday
                </td>
                <td>
                    Thursday
                </td>
                <td>
                    Friday
                </td>
                <td>
                    Saturday
                </td>
                <td>
                    Sunday
                </td>
            </tr>
            <tr>
<?php
for($counter = 1; $counter < $monthStart['wday']; $counter++)
{
?>
                <td>
                      
                </td>
<?php
}

$today = 0;

for($counter = $monthStart['wday']; $counter <= 7; $counter++)
{
    $today ++;
?>
                <td>
                    $today
                </td>
<?php
}
?>
            </tr>
<?php
    $totalWeeks = floor(($monthEnd['mday'] - $today) / 7);
    
    for($counter = 0; $counter < $totalWeeks; $counter++)
   {
        echo '<tr>';
        
        for($i = 0; $i < 7; $i++)
       {
            $today ++;
            echo "<td>$today</td>";
       }
        echo '</tr>';
    }

   if($today < $monthEnd['mday'])
  {
        echo '<tr>';
        
        for($i = 0; $i < 7; $i++)
       {
            $today++;

            if($today <= $monthEnd['mday'])
            {
                echo "<td>$today</td>";
            } else {
                echo '<td> </td>';
            }
        }
        
        echo '</tr>';
    }
?>


Did you like this article? There are hundreds more.

Comments:
bizboy12
2011-05-17 05:11:43
I found some free PHP calendar scripts listed on the page!
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
elasolova
My PHP Projects on Sat 26th Sep 10am
I have been developing PHP applications for almost a year now. I have developed three projects. One is a simple trivia game. The other is a question-answer based community at http://www.javaist.com/quans . The last one is a programming challenge site just
countll
Blog entry for Thu 25th Oct 7am on Thu 25th Oct 7am
soo nu on this wicked world of NET. just decided to dive in today..hope friend aroun here can help

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Test of experience (hopefully) by AcidIce

Things you're only likely to know if you've actually written a lot of PHP before :)


     
Your Ad Here
 
Copyright Open Source Institute, 2006