Building a Simple Monthly Calendar

In this tutorial we are going to learn how to build a simple calendar in PHP. You have seen many of those calendar date pickers mostly in Javascript. This PHP based one has a slightly different purpose - instead of populating an input field, it outputs links for the different dates and can be used for example as a monthly archive in a dynamic DB-driven site.

We will build this calendar following good coding practices and keeping the login in one PHP file and the presentation in one HTML file.

Let's start:

First we need to define the current date OR a date coming by POST after the calendar form is submitted:


PHP getdate() function returns an array of year, month and day. We assign this to $today variable and use it to define the calendar day, year and month - if they are coming from POST, the POST values have priority - otherwise we use $today values.

Then we need to define the number of days in the given month. We use function cal_days_in_month() for that:

$numdays=cal_days_in_month(CAL_GREGORIAN, $month, $year);

Now there's one specific. We don't want the calendar rows to be different number in different month. For that we'll make sure the calendar looks like the printed calendars and like this event calendar software by defining the week start day:

// calculate 'extra' days - these which need to come on the first
// line - like 30-31- -- - -- - -- - -- - 1

That's it, now we only need to include the design template:


The rest is easy - display year and month dropdows:

<select name="year" onchange="this.form.submit();">
if($i==$year) $selected='selected';
else $selected='';
echo "<option $selected value=$i>$i</option>";
<select name="month" onchange="this.form.submit();">
if($i==$month) $selected='selected';
else $selected='';
echo "<option $selected value=$i>".$lmonths[$months[$i]]."</option>";
// end PHP

And the calendar itself in a HTML table:

if($extra<0) $extra=0;
// ...
// the full code can be downloaded at the bottom

And your calendar is ready! You can download the free Microcal script that uses this same code here


  1. In this tutorial,code is very short that's more important things.

  2. Thanks for this useful post. For more visit: examanswer24.com

  3. Thanks for the nice post.
    BUT Why you make it mc_template.html instead of mc_template.php?
    My antivirus telling that there is an static files contains malicious code. I was checked & found that u write PHP code in HTML file & therefore my antivirus giving me alert.

  4. Ad Free Post | Ad your website advertise | Post Free AD on msnadz.com

  5. nice share, you can submit your source on our free community http://pasarkode.com/all.code and submit your source code here http://pasarkode.com/submiting.code for promotion your self

  6. If you want to see some more options please visit here: Web Designing Companies in Delhi

  7. My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai

  8. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

  9. This blog is really great. The information here will surely be of some help to me. Thanks!. November Calendar 2018 Printable

  10. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Best Devops Training in pune
    Microsoft azure training in Bangalore
    Power bi training in Chennai


  11. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training
    Selenium training in bangalore

  12. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!
    python training in rajajinagar
    Python training in bangalore
    Python Online training in usa

  13. Thanks For Sharing The Information Please Keep Updating us Information Shared Is Very Valuable Time Just Went On Reading The Article Python Online Training AWS Online Training Data Science Online Training Data Science Online Training Hadoop Online Training

  14. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live.
    IT Institute in KK nagar | angularjs training in chennai | dot net training in chennai | Web Designing Training in Chennai

  15. Really Very Infromative Post , Thanks For Sharing The Information With Us.
    Best AWS Training Institute in Hyderabad

  16. Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.

    Apache Spark Training in Pune
    Spark Training Institute in Pune