Posted By

SmpleJohn on 12/04/09


Tagged

php calendar


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

SmpleJohn
farondomenicgmailcom
Michael01
BrianCoyDesign


Calendar Function


 / Published in: PHP
 

URL: http://smple.com/snipplr/calendar.php

This is using list items to create the calendar so it is very flexible with CSS. Right now it's just displaying dummy data, but you can easily integrate a mysql call into the loop.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>PHP Calendar</title>
  6.  
  7. <style type='text/css'>
  8. #calendar {width:900px; overflow:auto; margin:0 auto;}
  9. li {width:14%; height:120px; display:block; position:relative; float:left; border:#000 1px solid; border-width:1px 0 0 1px; overflow:hidden;}
  10. .last {border-right-width:1px;}
  11. .lastweek {border-bottom-width:1px;}
  12. .today {background:#ccc;}
  13. .today .date {background:#333; color:#fff; font-weight:bold;}
  14. .date {display:block; width:20px; height:20px; position:relative; top:0; left:0; text-align:center; border:#000 1px solid; border-width:0 1px 1px 0;}
  15. li p {padding:0 4px; margin:0;}
  16. </style>
  17.  
  18. </head>
  19.  
  20. <body>
  21.  
  22. <div id='calendar'>
  23. <?php
  24. function calendar($setdate) {
  25. $boom = explode("/",$setdate);
  26. $m = $boom[0];
  27. $y = $boom[1];
  28. $totaldays = date("t", strtotime($y . "-" . $m . "-01"));
  29. $prev = date("m/y",mktime(0,0,0,$m-1,1,$y));
  30. $next = date("m/y",mktime(0,0,0,$m+1,1,$y));
  31. $days = range (1, $totaldays);
  32.  
  33. $startday = date("w",mktime(0,0,0,$m,1,$y));
  34. $lastli = 6-(date("w",mktime(0,0,0,$m,$totaldays,$y)));
  35. $count = 1;
  36. $weeks = 1;
  37. $weekcount = ceil(($startday + $totaldays)/7);
  38. if($startday==0){
  39. $weekcount++;
  40. }
  41.  
  42. echo "<div id='controls'><a href='?caldate=$prev' id='prevmonth' class='left'>$prev - prev</a> | $setdate | <a href='?caldate=$next' id='nextmonth' class='right'>next - $next</a></div>";
  43. for($i=0; $i<$startday; $i++){
  44. echo "<li class='blank'></li>";
  45. $count++;
  46. }
  47. foreach ($days as $value) {
  48. echo "<li class='";
  49. if ($count == 7){
  50. echo "last ";
  51. $count = 1;
  52. } else if ($count == 1){
  53. $weeks++;
  54. $count++;
  55. } else {
  56. $count++;
  57. }
  58. if ($weeks == $weekcount){
  59. echo "lastweek";
  60. }
  61. if (date('y') == $y && date('m') == $m && date('j') == $value){
  62. echo " today";
  63. }
  64. echo "'><div class='date'>$value</div><p>Title 1<br />Title 2</p></li>";
  65. }
  66. for($i=0; $i<$lastli; $i++){
  67. if ($count == 7){
  68. echo "<li class='blank last lastweek'></li>";
  69. $count = 1;
  70. } else {
  71. echo "<li class='blank lastweek'></li>";
  72. $count++;
  73. }
  74. }
  75. }
  76.  
  77. if (empty($_REQUEST['caldate'])){
  78. $date = date("m/y");
  79. } else {
  80. $date = $_REQUEST['caldate'];
  81. }
  82.  
  83. calendar($date);
  84. ?>
  85. </div><!-- calendar -->
  86.  
  87. </body>
  88. </html>

Report this snippet  

You need to login to post a comment.