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.


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?
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
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!
can you all give me the complete script to create the calendar????
@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.
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
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
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
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.
Anka!
Could you please tell us how you solved your “next month”-problem!
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
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.
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