4+ PHP Calendar Programs

This post contains a total of 4+ PHP Calendar Program Examples with Source Code. All these Calendar Programs are made using PHP.

You can use the source code of these examples with credits to the original owner.

Related Posts

PHP Calendar Programs

1. Basic PHP Calendar

Made by Bruno Félix. Source

Basic PHP Calendar
<?php
    function line($week) {
        $line = '<tr>';
        for ($i = 0; $i <= 6; $i++) {
            if (array_key_exists($i, $week)) {
                $line .= "<td>{$week[$i]}</td>";
            } else {
                $line .= "<td></td>";
            }
        }
        $line .= "</tr>";
        
        return $line;
    }
    
    function calendar() {
        $calendar = '';
        $day = 1;
        $week = [];
        
        while ($day <= 31) {
            array_push($week, $day);
            
            if (count($week) == 7) {
                $calendar .= line($week);
                $week = [];
            }
            $day++;
        }
        $calendar .= line($week);
        
        return $calendar;
    }
?>
<h2>Basic Calendar</h2>
<table border="1">
    <tr>
        <th>S</th>
        <th>M</th>
        <th>T</th>
        <th>W</th>
        <th>T</th>
        <th>F</th>
        <th>S</th>
    </tr>
    <?php echo calendar(); ?>
</table>

2. Calendar with Styling

Made by Koel Sen. Source

Calendar with Styling
<style>
    calendar { border-left:10px solid lightblue; width:100%;height:50%;}
    * html .day, * html .day-np { height:180px;}
    .day:hover { background:green; }
    .day-np { background:orange; min-height:180px; } 
    .day-head { background:red; color:yellow; font-weight:bold; text-align:center; padding:5px; width:280px; border-top:31px solid violet; border-right:1px solid violet;}
    .day-num { background:salmon; padding:5px; color:blue; font-weight:bold; float:right; margin:-5px -5px 0 0; width:20px; text-align:center; }
    .day, .day-np { padding:5px; border-bottom:1px solid black; width:80px;  border-right:1px solid black; }
    .day { color:blue; font-weight:bold; }
</style>
<?php
function draw_calendar($month, $year) {

    $cal = '<table cellpadding="0" cellspacing="0" class="calendar" width="100%"height="100%" border color="red">';
    $headings = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    $cal .= '<tr class="row">
             <td class="day-head">'
             .implode('</td><td class="day-head">',$headings).'
             </td></tr>';

    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $dates_array = array();
    
    $cal .= '<tr class="row">';

    for($x = 0; $x < $running_day; $x++) {
        $cal .= '<td class="day-np"> </td>';
        $days_in_this_week++;
    }

    for($list_day = 1; $list_day <= $days_in_month; $list_day++) {
        $cal .= '<td class="day" align="center">';
        $cal .= $list_day;
        $cal .= str_repeat('<p> </p>', 2);
        $cal.= '</td>';
        if($running_day == 6) {
            $cal .= '</tr>';
            if(($day_counter+1) != $days_in_month) {
                $cal .= '<tr class="row">';
            }
            $running_day = -1;
            $days_in_this_week = 0;
        }
        $days_in_this_week++; 
        $running_day++; 
        $day_counter++;
    };

    if($days_in_this_week < 8) {
        for($x = 1; $x <= (8 - $days_in_this_week); $x++) {
            $cal .= '<td class="day-np"> </td>';
        }
    }
    $cal .= '</tr>';
    $cal .= '</table>';
    
    return $cal;
}

//Draw calendar for the current month/year
echo draw_calendar(date("m"), date("Y"));

?>

3. Working PHP Calendar

Made by Alena Yakutova. Source

Working PHP Calendar
<!DOCTYPE html>
    <html>
        <head>
            <title>
            </title>
        </head>
    <body>
    <?php
    $mouth = date('n');
    
    $mouths = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'];
    if ($i = $mouth -1){
        echo $mouths[$i];
    }
     ?>, 
     <?php 
     $year = date('Y');
     echo $year;
     ?> год
     
     <br>
     
<?php
function draw_calendar($mouth,$year,$action = 'none'){

//display the header of the calendar
   $calendar = "<table border = '1'>"; $names_day=['Пн','Вт','Ср','Чт','Пт','Сб','Вс'];
    for($day = 0; $day < count($names_day); $day++){
    
    //select the weekend
    if (($day == 5) || ($day == 6)){
        $calendar .= "<th style = 'background-color:red'>" .$names_day[$day]."</th>";
    }else{
    
    //we deduce other days
    $calendar .= "<th>" .$names_day[$day]."</th>";   
      }
  }
  // today
  $now_day = date ('j'); 
  
 //determine the beginning of the week
  $running_day = date ('w',mktime(0,0,0,$mouth,1,$year));
  
  //ordinal number of the day of the week
  $running_day = $running_day - 1;
  if ($running_day == -1){
      $running_day = 6;
  }
  
  
  $days_in_mouth = date('t',mktime(0,0,0,$mouth,1,$year));
  $day_counter = 0;
  $days_in_this_week = 1;
  $dates_array = array();
  
  //output empty cells and start a new line
  $calendar .= "<tr>";
  
  for ($i = 0; $i < $running_day; $i++){
      $calendar .= "<td> </td>";
      $days_in_this_week++;
  }
 
  //output number
  for ($list_day = 1; $list_day <= $days_in_mouth; $list_day++){
      
      //select the weekend
      if($running_day != 0 && (($running_day % 5 == 0) || ($running_day % 6 == 0))){
        
        $calendar .= "<td style = 'color:red'>" . $list_day . "</td>";
      } elseif ($now_day == $list_day){
      
      
          //highlight today
          $calendar .= "<td style = 'background-color:gray'>" . $list_day . "</td>";
      } else {
          
          //other days
      $calendar .= "<td>" . $list_day . "</td>";
      }
      
      if ($running_day == 6){
          $calendar .= "</tr>";
          
          //if the day is not the last, start a new line
          
          if(($day_counter +1) != $days_in_mouth){
              $calendar  .= "<tr>";
          }
          
          // reset the counters
          $running_day = -1;
          $days_in_this_week = 0;
      }
      $days_in_this_week++;
      $running_day++;
      $day_counter++;
  }
        
        //output empty cells at the end of the last week
       if($days_in_this_week < 8) {
           for ($x = 1; $x <= (8 - $days_in_this_week); $x++){
               $calendar .= "<td> </td>";
           }
       }
    $calendar .= "</tr>";
    
    $calendar .= "</table>";
    
    return $calendar;
}
?>

<?php 
    $year = date('Y');
    $mouth = date ('n');
    
    echo draw_calendar($mouth,$year);
?>
    </body>
</html>

4. Simple Calendar

Made by abdimahad Abdullahi. Source

Simple Calendar
<?php
function draw_calendar($month, $year) {

    $cal = '<table cellpadding="0" cellspacing="0" class="calendar" width="100%">';
    $headings = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    $cal .= '<tr class="row">
             <td class="day-head">'
             .implode('</td><td class="day-head">',$headings).'
             </td></tr>';

    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $dates_array = array();
    
    $cal .= '<tr class="row">';

    for($x = 0; $x < $running_day; $x++) {
        $cal .= '<td class="day-np"> </td>';
        $days_in_this_week++;
    }

    for($list_day = 1; $list_day <= $days_in_month; $list_day++) {
        $cal .= '<td class="day" align="center">';
        $cal .= $list_day;
        $cal .= str_repeat('<p> </p>', 2);
        $cal.= '</td>';
        if($running_day == 6) {
            $cal .= '</tr>';
            if(($day_counter+1) != $days_in_month) {
                $cal .= '<tr class="row">';
            }
            $running_day = -1;
            $days_in_this_week = 0;
        }
        $days_in_this_week++; 
        $running_day++; 
        $day_counter++;
    };

    if($days_in_this_week < 8) {
        for($x = 1; $x <= (8 - $days_in_this_week); $x++) {
            $cal .= '<td class="day-np"> </td>';
        }
    }
    $cal .= '</tr>';
    $cal .= '</table>';
    
    return $cal;
}

//Draw calendar for the current month/year
echo draw_calendar(date("m"), date("Y"));

?>

5. PHP Dropdown Calendar

Made by Leah. Source

PHP Dropdown Calendar
<?php

# 3 drop down menus for html
# days, months, years

$months = [1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];

$days = range(1, 31);

$years = range(2017, 2027);

# days
	echo "<select name=\"days\">\n";

	foreach ($days as $value) {
		echo "<option value=\"$value\">$value</option>\n";
	}

	echo "</select>\n";

# months
	echo "<select name=\"month\">\n";
	
	foreach ($months as $k => $v) {
		echo "<option value=\"$k\">$v</option>\n";
	}

	echo "</select>\n";

#years
	echo "<select name=\"years\">\n";

	foreach ($years as $value) {
		echo "<option value=\"$value\">$value</option>\n";
	}

	echo "</select>\n";

?>