Skip to main content

Simple PHP Calendar

Posted in

Posted it on my blog, but I decided that I should share it here as well, for those of you that need a simple php calendar.

  1. <?php
  2. // This gets today's date
  3. $date = time ();
  4.  
  5. // This puts the day, month, and year in seperate variables
  6. $day  = date('d', $date);
  7. $month= date('m', $date);
  8. $year = date('Y', $date);
  9.  
  10. // Here we generate the first day of the month
  11. $first_day = mktime(0,0,0,$month, 1, $year);
  12.  
  13. //This gets us the month name
  14. $title = date('F', $first_day);
  15.  
  16. //Here we find out what day of the week the first day of the month falls on
  17. $day_of_week = date('D', $first_day);
  18.  
  19. //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
  20. switch($day_of_week) {
  21.   case "Sun": $blank = 0; break;
  22.   case "Mon": $blank = 1; break;
  23.   case "Tue": $blank = 2; break;
  24.   case "Wed": $blank = 3; break;
  25.   case "Thu": $blank = 4; break;
  26.   case "Fri": $blank = 5; break;
  27.   case "Sat": $blank = 6; break;
  28. }
  29.  
  30. // We then determine how many days are in the current month
  31. $days_in_month = cal_days_in_month(0, $month, $year);
  32. ?>
  33.  
  34. <table cellpadding="0" cellspacing="0">
  35.   <tr>
  36.     <th colspan="7"><?php print $month; ?> <?php print $year; ?></th>
  37.   </tr>
  38.   <tr>
  39.     <td>Sun</td>
  40.     <td>Mon</td>
  41.     <td>Tue</td>
  42.     <td>Wed</td>
  43.     <td>Thu</td>
  44.     <td>Fri</td>
  45.     <td>Sat</td>
  46.   </tr>
  47.    <?php $day_count = 1;// This counts the days in the week, up to 7 ?>
  48.   <tr>
  49.   <?php while ($blank > 0): //first we take care of those blank days ?>
  50.     <td>&nbsp;</td> <?php $blank = $blank-1; $day_count++; ?>
  51.   <?php endwhile; ?>
  52.   <?php $day_num = 1; //sets the first day of the month to 1 ?>  
  53.  
  54.   <?php while ($day_num <= $days_in_month): //count up the days, until we've done all of them in the month ?>
  55.     <td><?php print $day_num; ?></td>
  56.     <?php $day_num++; $day_count++; ?>
  57.     <?php if ($day_count > 7): //Make sure we start a new row every week ?>
  58.   </tr>
  59.   <tr>
  60.     <?php $day_count = 1; ?>
  61.     <?php endif; ?>
  62.   <?php endwhile; ?>
  63.    
  64.   <?php while ($day_count >1 && $day_count <=7): //Finaly we finish out the table with some blank details if needed ?>
  65.     <td>&nbsp;</td>
  66.     <?php $day_count++; ?>
  67.   <?php endwhile; ?>
  68.   </tr>
  69. </table>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

If you need more help on the topic feel free to add a comment or create a new post on the Forum