1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of Angela Bradley
Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

Simple PHP Calendar

Friday September 15, 2006

Generating a simple PHP calendar isn't as hard as you might think. Our tutorial demonstrates how to do this. Once you have mastered a simple calendar, you can then apply the same concepts to more complex calendars you may need in your programming.

Comments
September 18, 2006 at 5:18 am
(1) Loretta says:

This will come in handy :) Great tutorial. How difficult would it be to add events to the calendar or to put a star or marking on certain days?

August 23, 2007 at 4:35 am
(2) Robert says:

How hard would it be to make the day numbers links to a database? Like when you click on it to send http://yoursite/locate.php?day=5&month=3&year=2007&weekday=wednesday

September 7, 2007 at 7:32 am
(3) Rob Dickie says:

Thank you for this. Finally a calendar that does exactly what is says on the tin. Free from any complication! Easily modified to suit any project!

March 3, 2009 at 4:09 am
(4) alex yong says:

can you all give me the complete script to create the calendar????

March 4, 2009 at 9:31 pm
(5) David says:

@alex: just copy every pieces of code from each page and then you’ll get the complete script.

To know which day of the week is the first days of month, you can use date(’w', $first_day) and you will get the same result as the switch would return in a single line of code instead of a loop.

March 8, 2009 at 7:33 am
(6) Anka says:

hello, first of all sorry for my english..
it`s great script, thank You :)
Would You explain me how can I add navigation between month? Do I need change something in this script or just add something like: $next_month = $month + 1; and if ($next_month = 13) { $next_month = 1; $year = $year + 1; } ? I should propably add if(isset($_GET['month'])) $month = date(’m'); too but I don’t know how to print/echo this? It’s not working, however I can see in address bar e.g localhost/calendar.php?month=4&year=2009 but calendar is not displayed properly (I mean: first day of next/previous month always amount 1).
Would You please be so kind and explain me this?
best regards for all :)
Anka

March 8, 2009 at 9:31 am
(7) Anka says:

I`m sorry I bother You. I solved this already! :)
have a nice day, best regards :)
Anka
PS. I hope my english was more understandable than php programming ;)

March 8, 2009 at 4:15 pm
(8) Jason says:

There’s a slight error in the script. In perfect months, i.e. months that end on a Saturday, the script creates an unnecessary new row with no columns in it. Replacing the line near the end that says “if ($day_count > 7)” with “if ($day_count > 7 && $day_num

March 8, 2009 at 4:23 pm
(9) Hiroshi says:

There is a small error in the week script. It goes along and creates a new row each time the day hits a saturday. Problem is, when it hits a saturday it doesn’t check if there is actually another day. so in months that end on a saturday, it creates a row, but the row does not contain any s. It’s an easy fix just change “if($day_count > 7)” to “if ($day_count > 7 && $day_num friends.

March 17, 2009 at 6:52 am
(10) Ann-Britt says:

Anka!

Could you please tell us how you solved your “next month”-problem!

March 26, 2009 at 4:32 pm
(11) Ian says:

Hi Ann-Brit…

w.r.t the next / previous month problem, I’ve had a go at it – I know it is very ugly code (newbie) … but it works. I wrapped the calender php into a function:

function SimpleCalendar($year, $month, $day)
{
.
.
}

then added the following code – which stores the current year and month in $_GET (via hidden fields in forms). When a submit button is pressed, the php file is re-loaded and uses the year & month that from the $_GET arguments:

if(isset($_GET["back"]))
{
$month = $_GET["CurrentMonth"];
$year = $_GET["CurrentYear"];
if($month == 1)
{
$month = 12;
$year = $year-1;
}
else
{
$month=$month-1;
}
}
else if (isset($_GET["next"]))
{
$month = $_GET["CurrentMonth"];
$year = $_GET["CurrentYear"];
if($month == 12)
{
$month = 1;
$year = $year+1;
}
else
{
$month=$month+1;
}
}
else
{
// use the current date
$date =time () ;
$day = date(’d', $date) ;
$month = date(’m', $date) ;
$year = date(’Y', $date) ;
}

// call the calendar code
SimpleCalendar($year, $month, $day);

// construct 2 hidden forms (for previous and // next submit buttons

echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;

echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;

hope it gives you some ideas
Ian

May 8, 2009 at 6:37 am
(12) g says:

I would like to see a sample where the days that are not in the current month are not BLANK.

So display 30 | 31 | 1 | 2 | 3 |, etc.

August 4, 2009 at 6:41 am
(13) dowhile says:

Thanks for the tutorial
when I tried it it gave me undefined variable blank
and points to While ( $blank > 0 )
pls can someone help me remove the notice
thanx

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.