Jump to content

Calender Script...


phpSensei

Recommended Posts

Cooked it up in 1 hour and 30 mins

 

demo: http://filecrave.com/scripts/PHPcalendar.php

 

HTTP_VARS: PHPcalendar.php?month=&year=

 

month format must be: 1-12

year format must be: YYYY

 

<?php
	  
		  #################################################
		  ## Author: PHPSensei
		  ## Script Name: Calender
		  ## Date/Time: Sunday, December 21, 2008 11:42 pm
		  #################################################
		  
		  function generateCalender(){
		  // Variables we are going to need throughout the script
		  // To generate the calender I will be setting up variables that will be useful
		  $now = array();
		  $now['month']  = date('m'); // current month 01-12
			  $now['day']    = date('j'); // current day / we can also use 'd'
		  $now['year']   = date('Y'); // current year
		  
		  // The variables below are HTTP VARS given to the script to change our calendar dates
		  $get = array();
		  $get['year']  = (int)$_GET['year'];
		  $get['month'] = (int)$_GET['month'];
		  
		  if((isset($get['year']))&&(preg_match("/[0-9]{4}/",$get['year']))){ $now['year'] = $get['year']; }
		  if((isset($get['month']))&&(preg_match("/[0-9]/",$get['month']))){ if($get['month'] > 0 || $get['month'] <= 31){ $now['month'] = $get['month']; }}
		  
		  $month_num = array(1 => 'January', 2 => 'February',3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August',  9 => 'September', 
		  10 => 'October', 11 => 'November', 12 => 'December');
		  // Search for to see if the month is valid
		  $check_monthArray  = array_key_exists($now['month'],$month_num);
		  if($check_monthArray == false){ $now['month'] = date('m'); }
		  $check_date = checkdate($now['month'],01,$now['year']);
		  if($check_date == false){  $now['month'] = date('m'); $now['year'] = date('Y'); }
		  $month_name = $month_num[$now['month']]; // 01 to Jan
		  $first_day = mktime(0,0,0,$now['month'], 1, $now['year']);
              $lastday = cal_days_in_month(0, $now['month'], $now['year']);
		  $days_of_week = date('D', $first_day);
		  switch($days_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;
              }
		  $counter = 1;
		  // Bottom code here is a nowdoc of the top part of the entire table
		  // the stamp YEAR is going to be replaced by our script// the stamp MONTH is going to to be replaced by our script
		  $html_top = '<table id="cal_border" width="100%" border="0" cellspacing="0" cellpadding="0">
             <tr>
             <td align="center"></td> 
             </tr>
             <tr>
             <td height="35" id="cal_border_bottom" align="center" bgcolor="#DEE4EB"><div id="month_txt">'.$month_name.' - '.$now['year'].'</div></td> 
             </tr>
             <tr>
             <td align="center"><table width="100%" border="0" id="day_txt" cellspacing="1" cellpadding="0"><tr>';
		 print $html_top; // end heredoc
             
		 while($blank > 0){echo '<td></td>'; $blank = $blank-1; $counter++;}
		 $firstdayofmonth = 1;
             while ($firstdayofmonth <= $lastday){
             echo "<td height=\"40px\" align=\"center\" bgcolor=\"#F0F5F7\"> $firstdayofmonth </td>
             ";
             $firstdayofmonth++;
             $counter++;
             
             //Make sure we start a new row every week
             if ($counter > 7){
             echo "<tr></tr>";
             $counter = 1;
             }
             }while ( $counter >1 && $counter <=7 )
             {
             echo "<td></td>";
             $counter++;
             }  
         // Bottom code here ends the entire table
         $html_bottom = ' 
		 </tr></table></td>
             </tr>
             </table>'; // end nowdoc


		 print $html_bottom;

		 }//end calender function
?>

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.