11 PHP 101 Screencast - Building A PHP Calendar

In this video I will show you how to build a PHP Calendar. The code for this is below.
	
echo '<link rel="stylesheet" href="calendar.css" style="text/css">';
// Store todays date
$today_date = time(); 
// Split into day month and year 
$day = date('d', $today_date); $month = date('m', $today_date); $year = date('Y', $today_date);
// Make first day of the month 
$first_day = mktime(0,0,0,$month, 1, $year); 
// Get name of the month 
$title = date('F', $first_day); 
// What day of the week is the first day of the month 
$day_of_week = date('D', $first_day); 
// Work out how many blank days to put in it 
switch($day_of_week) { case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } 
// How many days in the month in total 
$days_in_month = cal_days_in_month(0, $month, $year); 
// Calendar header 
echo '

<table border="0" cellspacing="0" cellpadding="0" width="294px">
  '; echo '<tr>
    <th colspan="7">
      ' .$title . ' ' . $year. '
    </th>
  </tr>'; echo '
  
  <tr>
    <td width="42" class="weekend">
      S
    </td>
    
    <td width="42">
      M
    </td><td width="42">T</td>
    
    <td width="42">
      W
    </td>
    
    <td width="42">
      T
    </td><td width="42">F</td>
    
    <td width="42" class="weekend">
      S
    </td>
  </tr>';

  // Count days inserted

  $day_count = 1;

  echo '  
  <tr>
    '; // Blank out the days at the begining that we don\'t need 
    while ( $blank > 0 ) {
      if ($day_count == 1 || $day_count == 7) {
        echo '<td class="weekend"> </td>';
      } else {
      echo '
    <td>
    </td>'; } $blank = $blank-1; $day_count++; } 
    // Make first day of the month = 1 
    $day_num = 1; 
    // Put all days on the page 
    while ($day_num <= $days_in_month) { if ($day_count == 1 || $day_count == 7) { if ($day_num == $day) { echo '
    
    <td class="weekend today">
      <a href="./?d=' .$day_num. '&m=' .$month. '"> ' .$day_num. ' </a>
    </td>'; } else { echo '
    
    <td class="weekend">
      <a href="./?d=' .$day_num. '&m=' .$month. '"> ' .$day_num. ' </a>
    </td>'; } } else { if ($day_num == $day) { echo '
    
    <td class="today">
      <a href="./?d=' .$day_num. '&m=' .$month. '"> ' .$day_num. ' </a>
    </td>'; } else { echo '
    
    <td>
      <a href="./?d=' .$day_num. '&m=' .$month. '"> ' .$day_num. ' </a>
    </td>'; } } $day_num++; $day_count++; 
    // Seperate the week out onto new lines 
    if ($day_count > 7) { echo '
  </tr>
  
  <tr>
    '; $day_count = 1; } } 
    // Blank out days not needed at the end of the month 
    while ($day_count > 1 && $day_count <= 7) { if ($day_count == 1 || $day_count == 7) { echo '<td class="weekend">
    </td>'; } else { echo '
    
    <td>
    </td>'; } $day_count++; } 
    // End the table 
    echo '
  </tr>
</table>';  
And the CSS

table, tbody, td { 
border: 1px solid #DDD;
text-align: center;
}

.weekend { 
 background: #EEE;
}

.today {
  background: #000;
}

.today a {
  color: #FFF;
}

a {
  color: #000;
  display: block;
}

comments powered by Disqus