How to code calendar in PHP



Starting with Monday version

A Calendar with PHP (starting with Monday) Introduction In this video, I'm going to share how to devel...



A Calendar with PHP & MySQL Introduction Welcom...


Introduction & Demo

Today, I’m going to share “How to code calendar in PHP” videos.

Before you watch the video, please check the demo from here.

Coding calendar contains a lot of coding basics and functions like loops, mktime function, GET etc…
I think it will be helpful to learn how PHP works with HTML and how to deal with time functions.

Also, I used Bootstrap and Google Fonts in this tutorial.
Bootstrap helps us to make beautiful layout easily, and you can design your site without special CSS knowledge.

Once you’ve done, you can print it out and use as your schedule memo!

Happy Coding 🙂



Part1 : HTML & CSS

How to Create a Calendar in PHP ( Part1 HTML & CSS )


Part2 : PHP

How to Create a Calendar in PHP ( Part2 PHP )


Sample Code (07/14/2018 Updated)



Google Fonts: (I used “Noto Sans” in the video.)


Working as a freelance programmer in Japan. I try to make coding as simple as possible. Hope you enjoy coding 🙂
  • rey anthony omiple

    hey there.. i have a question, i just follow your code but my current date or day is not in active? and I also see your DEMO but its just desame, what could be the problem ?

    can you tel me how I can fix it ?

    • Sara

      Thank you for letting me know! That’s my mistake.
      Please change $today like this.

      $today = date(‘Y-m-j’, time()); // change “d” to ” j”

      Hope this works 🙂

  • ammar

    hello there, thankyou for the good tutorial on how to make a calendar :). but may i know how i can insert notes on a certain date? Like a reminder? Will i be able to do that? Im quite new to prgramming so i still dont know how can i make something like that works.. Will be glad if you can help! 🙂

    • Sara

      Hi ammar,
      You can insert your schedules by connecting with MySQL database. I’m working on new calendar tutorial series which you can add, edit, and delete schedules. Although it’ll not be a free version, try it if you’re interested 😉
      Thank you for watching my videos!

  • CYN

    Hi, do I need wamp for it to work?

    • Sara

      Hi CYN,
      Yes, you need a local or web server to run PHP file. Please install WAMP or XAMPP or MAPP… whichever you like.

  • Kedar Shrestha

    short and sweet

  • Daniel Elskamp

    How to change the Script, if monday the first day in the week?

    • Sara

      Hi Daniel, try changing these 4 lines.

      // $str = date(‘w’, mktime(0, 0, 0, date(‘m’, $timestamp), 1, date(‘Y’, $timestamp)));
      $str = date(‘N’, mktime(0, 0, 0, date(‘m’, $timestamp), 1, date(‘Y’, $timestamp)));
      *N returns 1:Mon 2:Tue … 7:Sun

      // $week .= str_repeat(”, $str);
      $week .= str_repeat(”, $str – 1);

      // if ($str % 7 == 6 || $day == $day_count) {
      if ($str % 7 == 0 || $day == $day_count) {

      // $week .= str_repeat(”, 6 – ($str % 7));
      $week .= str_repeat(”, 7 – ($str % 7));

      Hope this helps 🙂

      • Daniel Elskamp

        Thanks Man its working 🙂 Nice Job

        • Sara

          It’s my pleasure, glad it worked 🙂

          • Daniel Vong Elskamp Her

            Sorry Sara. You make a nice job 🙂 Ma’am

          • Sara

            Hi Daniel, Never mind 😀

        • Sakunne Svensson

          Sara seems not to be a Man but Woman

          • Sara

            Hi Sakunne, yes, I’m a woman. Curiously enough, some people think I’m a man 🙂
            Thanks for your comment!

  • Yohan 8077

    I can’t get the Today portion to work. Am I missing something? Thank You in advance.


    if (isset($_GET[‘ym’])) {
    $ym = $_GET[‘ym’];
    } else {
    $ym = date(‘Y-m’);

    $timestamp = strtotime($ym,”-01″);
    if ($timestamp === false) {
    $timestamp = time();

    $today = date(‘Y-m-d’, time());

    $html_title = date(‘F | Y’, $timestamp);

    $prev = date(‘Y-m’, mktime(0, 0, 0, date(‘m’, $timestamp)-1, 1, date(‘Y’, $timestamp)));
    $next = date(‘Y-m’, mktime(0, 0, 0, date(‘m’, $timestamp)+1, 1, date(‘Y’, $timestamp)));

    $day_count = date(‘t’, $timestamp);

    $str = date(‘w’, mktime(0, 0, 0, date(‘m’, $timestamp), 1, date(‘Y’, $timestamp)));

    $weeks = array();
    $week = ”;

    $week .= str_repeat(”, $str);

    for ( $day = 1; $day <= $day_count; $day++, $str++) {
    $date = $ym.'-'.$day;
    if ($today == $date) {
    $week .= '’.$day;
    } else {
    $week .= ”.$day;
    $week .= ”;

    if ($str % 7 == 6 || $day == $day_count) {
    if($day == $day_count) {
    $week .= str_repeat(”, 6 – ($str % 7));
    $weeks[] = ”.$week.”;
    $week = ”;

    • Sara

      Hi Yohan, thank you for watching 🙂
      Please change
      $today = date(‘Y-m-d’, time());
      $today = date(‘Y-m-j’, time());
      Sorry, it’s my mistake. Hope this works.

      • Yohan 8077

        Perfect. Thank You Very Much.

  • Yohan 8077

    Let me be more clear, it won’t write the td class=”today”

  • [d!p]

    Hi Sara,

    Thanks for this code! It’s excellent; How was wondering, how to get the selected values visible.
    For example, I add on the $day value, with and a POST_ and then when you click on it, you get show the selected “day” for example 15. Thanks in advance

    • Sara

      Hi [d!p], thank you for your comment 🙂
      [you get show the selected “day” for example 15] means
      you want to change the color of the selected day, like ??
      If so, change this line
      $today = date(‘Y-m-j’, time());
      if (!empty($_POST[‘selected_date’])) {
       $today = date(‘Y-m-j’, strtotime($_POST[‘selected_date’]));
      } else {
       $today = date(‘Y-m-j’, time());

      * I assume that the format of $_POST[‘selected_date’] is Y-m-j, like 2018-06-7.
      Hope this helps.

  • Stian Skjelbred

    Great! Thank you for this. Really saved me a lot of time.

    • Sara

      Hi Stian, my pleasure! Thanks for your comment 🙂

  • Daniel Elskamp

    Hi Sara,

    i have a new Problem with the code. If the last day of the month is the last day of the week, a new blank line is created. Why? Look



    Empty Line or


    You can help me?

    • Sara

      Hi Daniel, thank you for finding my website 🙂
      if($day == $day_count)
      if($day == $day_count && 7 – ($str % 7) != 7) {
      I’ll create the sample code starting with Monday sometime soon.
      Hope this helps 🙂

      • Daniel Elskamp

        Thank you Sara,

        It working 🙂

        • Sara

          Glad it worked 🙂

  • John Del Miracle

    i’ll copy & paste your codes
    and this is the output
    can you help me pleasE?

    • Sara

      You can’t run PHP scripts in HTML file.
      Please change a file name “calendar.php” then place it in the folder of your localhost. Hope this helps 🙂

      • John Del Miracle

        nah, thanks sorry for being too crazy -_-”
        thank you!

        • Sara

          Never mind 🙂

  • David

    How do add an event in my code? Can you please help?