go top


  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

How to make a PHP calendar

Tuesday, 6th December, 2011  /  PHP Tutorials  /  146 Comments
In this tutorial you will learn how to build your own web based calendar using PHP. This calendar is made from two parts. On top there are the links to the previous and the next month, and below them is the calendar itself. It will show the selected month name with the year following and the days of the month bellow in a table view. You will need a good PHP editor to help you with your development or if you are experienced enough you can use a plain text editing software such as Notepad.

At the beginning we have to decide how we are going to pass our parameters. We can use either 'hidden' input fields in out html code or pass them in the URL. In this tutorial we will use the second approach. We are going to need two parameters - one for the "month" and one for the "year". We will also need an array with month names:

$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");

First we need to check if we have set up our parameters already. If we have not, we set them to the current month and year. (For detailed explanation of date() function please refer to PHP Manual ).

if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

Lets also set the variables that we are going to use in our calandar:

$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];

$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;

if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;

$cMonth and $cYear are used for the current month and year displayed on the calendar. For the "Previous" and "Next" links we will need the corresponding parameters. We set $prev_year and $next_year to the current one. Later we may have to change this but for now it is OK. We also have to set parameters for the next and previous months by adding and subtracting 1. Now is the catch. We have to check if our parameter has not gone over or down the limit. Since there are 12 months in a year if our parameter goes to 13 then it means that another year has passed by and we have to set our "month" parameter back to 1 ( January ) and add 1 to our "year" parameter. The other way around is when we go back in time and our "month" parameter goes to 0. Then we have to decrease our "year" parameter by 1 and set the month parameter to 12 ( December ). Now as we set our links for previous and next months we turn to how to build the actual calendar.

We create a table that will hold our calendar and add the links in one row. Then we add a table in a new row that will hold the days. We also include the month name and the year in the first on the new table. But because arrays are zero based, we need to subtract one from the "month" parameter value to get the correct name.

<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>  </td>
<td align="center">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>

Now we have to set the proper dates for our calendar. We have to make integer representation of the date so we can easily operate with it. Then we get the number of days of the selected month and the number representation (0 for Sunday through 6 for Saturday) of the first day of the month. All of these functions are fully explained in PHP Manual. Our loop that is going to print the dates starts at 0, because the days of the week start from 0 (Sunday). It has to loop through the number of days plus the offset of the first day of the month. We have to print new row for each week. We check this by modulus of the number of days in one week 7. If it equals 0 then it is the beginning of the week and we print open row tag <tr> and if it is the end of the week 6 we print close tag for this week </tr>. All we need to do is to check if the day that we print is before $startday. In this case we print empty tag. Otherwise we have to print the date. We make it by subtracting the $startday. we have to add one because we don't want our dates to start from 0.

$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>n";
if($i < $startday) echo "<td></td>n";
else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>n";
if(($i % 7) == 6 ) echo "</tr>n";

Now we add just the finishing touches of the html and we have finished the calendar.

Share on:

146 Comments to "How to make a PHP calendar"


Nelson / April 7, 2020 at 03:34 am

Thanks for the tutorial. Kindly provide the full code for testing


sakir / May 17, 2018 at 12:52 pm


nagma khazi

nagma khazi / October 11, 2017 at 15:29 pm

Any one plz send me the coding of calendar in php... In written type


Carolina / October 5, 2017 at 04:19 am

How to integrated Mysql events ??


Accumike / April 18, 2017 at 17:08 pm

Filter between 2 dates
I have used this great script for a while, adding in the database functionality suggested by Jarle Hagavei.
Now my customer wants to filter by date, regardless of month (eg 12/03/2017 to 24/04/2017) and still display results in calendar format, week by week.
This looks like a complicated request - does anyone know how to do this?


finest / August 20, 2016 at 09:59 am

I want multi selection of dates in calendar. How to add click event on every single date?


Scott / June 28, 2016 at 20:55 pm

I know this Calendar was created a long time ago and I thank the original Author and others for their work.

What I'm trying to do is loop data to get multiple events per day, Im setting this up on my amateur radio site and I have multiple nets that I have to attend to for Emergency Communications. Some days its 4 to 5 nets withing 3 hours. I tried adding the nets to a data table and looping with a while statement but it only pulls one and displays it.


Scott / June 28, 2016 at 20:57 pm


How to add the Current Week of the Year, IE: it's currently week 26 of 2016. I would liek to add that to the left side of the Calendar.

Sasho Valkanov

Sasho Valkanov / June 29, 2016 at 08:58 am

Hi Scott,

do you read the events from MySQL database or it's just an array that you created? Can you post the code here so I can take a look.

Current week can be printed using echo date("W");


Scott / June 30, 2016 at 01:13 am

I want the weeks to to count themselves up, IE: it's week 26 of 2016, next week is week 27, etc, etc. I figured out the loop so thats taken care of, now I'm trying to figure out how to put the next week in the next weeks row

Sasho Valkanov

Sasho Valkanov / June 30, 2016 at 09:30 am

You can use the first day for each week and use


to get week number for the week where date is.


Scott / June 30, 2016 at 16:58 pm

Yeah go ahead and run that and see what it gives you, Its week 26, not week 54

Sasho Valkanov

Sasho Valkanov / July 1, 2016 at 10:06 am

sorry, I made a mistake with the year. It should be 2016 and not 206 :)

echo date("W",strtotime("10-07-2016")); // prints week 27
echo date("W",strtotime("24-07-2016")); // prints week 29


Scott / July 1, 2016 at 19:13 pm

yes but how to loop that so it prints the current week every week, Week 26, 27, 28 etc


Accumike / April 18, 2017 at 17:22 pm

To show multiple events in the same day, add your SQL query below the 'days of the week' row and then your while loop inside the code that builds each day of the calendar.


youssef / November 5, 2015 at 16:45 pm

pleas ..if I want to make a current date of day in specific color like "red".
what should be I make ????????


mahe / October 1, 2015 at 13:02 pm

In calendar november 2015 date 1 is not shown why ??


abdulraheem_taha / August 27, 2015 at 01:34 am

Excellent example.. Thank you!

Add your comment


    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.

    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!

    Commercial PHP scripts