Posted By

koncept on 02/27/07


Tagged

php calendar


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

d4rk
vali29
skywalker
davidhorn
jamesming
nb109


Generate HTML calendar for dates


 / Published in: PHP
 

  1. <?php
  2. /**
  3.  * Generate HTML calendar based on date
  4.  * @author: Elliott White
  5.  * @author: Jonathan D Eisenhamer.
  6.  * @link: http://www.quepublishing.com/articles/article.asp?p=664657&seqNum=7&rl=1
  7.  * @since: December 1, 2006.
  8.  */
  9. if( function_exists( 'date_default_timezone_set' ) )
  10. {
  11. // Set the default timezone to US/Eastern
  12. date_default_timezone_set( 'US/Eastern' );
  13. }
  14.  
  15. // Will return a timestamp of the last day in a month for a specified year
  16. function last_day( $month, $year )
  17. {
  18. // Use mktime to create a timestamp one month into the future, but one
  19. // day less. Also make the time for almost midnight, so it can be
  20. // used as an 'end of month' boundary
  21. return mktime( 23, 59, 59, $month + 1, 0, $year );
  22. }
  23.  
  24. // This function will print an HTML Calendar, given a month and year
  25. function print_calendar( $month, $year, $weekdaytostart = 0 )
  26. {
  27. // There are things we need to know about this month such as the last day:
  28. $last = idate( 'd', last_day( $month, $year ) );
  29.  
  30. // We also need to know what day of the week the first day is, and let's
  31. // let the system tell us what the name of the Month is:
  32. $firstdaystamp = mktime( 0, 0, 0, $month, 1, $year );
  33. $firstwday = idate( 'w', $firstdaystamp );
  34. $name = date( 'F', $firstdaystamp );
  35.  
  36. // To easily enable our 'any day of the week start', we need to make an
  37. // array of weekday numbers, in the actual printing order we are using
  38. $weekorder = array();
  39.  
  40. for ( $wo = $weekdaytostart; $wo < $weekdaytostart + 7; $wo++ )
  41. {
  42. $weekorder[] = $wo % 7;
  43. }
  44.  
  45. // Now, begin our HTML table
  46. echo "<table><tr><th colspan=\"7\">{$name} {$year}</th></tr>\n";
  47.  
  48. // Now before we really start, print a day row:
  49. // Use the system to tell us the days of the week:
  50. echo '<tr>';
  51.  
  52. // Loop over a full week, based upon day 1
  53. foreach ( $weekorder as $w )
  54. {
  55. $dayname = date( 'D',
  56. mktime( 0, 0, 0, $month, 1 - $firstwday + $w, $year ) );
  57. echo "<th>{$dayname}</th>";
  58. }
  59.  
  60. echo "</tr>\n";
  61.  
  62. // Now we need to start some counters, and do some looping:
  63. $onday = 0;
  64. $started = false;
  65.  
  66. // While we haven't surpassed the last day of the month, loop:
  67. while ( $onday <= $last )
  68. {
  69. // Begin our next row of the table
  70. echo '<tr>';
  71.  
  72. // Now loop 0 through 6, for the days of the week, but in the order
  73. // we are actually going, use mod to make this work
  74. foreach ( $weekorder as $d )
  75. {
  76. // If we haven't started yet:
  77. if ( !( $started ) )
  78. {
  79. // Does today equal the first weekday we should start on?
  80. if ( $d == $firstwday )
  81. {
  82. // Set that we have started, and increment the counter
  83. $started = true;
  84. $onday++;
  85. }
  86. }
  87.  
  88. // Now if the day is zero or greater than the last day make a
  89. // blank table cell.
  90. if ( ( $onday == 0 ) || ( $onday > $last ) )
  91. {
  92. echo '<td>&nbsp;</td>';
  93. }
  94. else
  95. {
  96. // Otherwise, echo out a day & Increment the counter
  97. echo "<td>{$onday}</td>";
  98. $onday++;
  99. }
  100. }
  101.  
  102. // End this table row:
  103. echo "</tr>\n";
  104. }
  105.  
  106. // Now end the table:
  107. echo '</table>';
  108. }
  109.  
  110. // ========================================================
  111. // = Demo showing calendar for all months of current year =
  112. // ========================================================
  113.  
  114. // Output some formatting directives:
  115. echo '<style>table, td, th { border: 1px solid black; }</style>';
  116.  
  117. // Create an entire year calendar for 2006 with Monday as the first day:
  118. foreach( range( 1, 12 ) as $m )
  119. {
  120. print_calendar( $m, date( 'Y' ), 1 );
  121. echo '<br />';
  122. }
  123. ?>

Report this snippet  

You need to login to post a comment.