Products
Services
Contact Us

Scripts: Php :: Php date and time >> Library Article #5

Developer's Section

PHP Calendar
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (1820)
Favorites:

Create a simple calendar in a matter of seconds with the following tutorial.

In this tutorial we will be providing a class that you can use to create a simple calendar.

Example:



The Code for the Calendar is as follows:

In File: myCalendar.php

Let's first create the constructors and all of the date and time functions required to output this calendar.

 Code Snippet 1

<?php

/*  
 *  myCalendar Class  
 *  
**/  

class myCalendar
{
  var $month = 0;
  var $day = 0;
  var $year = 0;
  var $Months = Array("January", "February", 
  "March", "April", "May", "June", "July", 
  "August", "September", "October", "November", "December"); 
  
  
  function myCalendar($thisMonth, $thisDay, $thisYear)
  {    
    $this->month = $thisMonth;
    $this->day = $thisDay;
    $this->year = $thisYear;
  }  
    
  function getDayOfMonth($month, $day, $year)
  {
    $myresult = date("l", mktime(0, 0, 0, $month, $day, $year));
    
    return $myresult;
  }  
  
  
  function getMonthName($month, $day, $year)
  {
    $myresult = date("F", mktime(0, 0, 0, $month, $day, $year));
    
    return $myresult;
  }  
  
  function getWeekDay($day)
  {
      $myresult = 0;   
    switch ($day) {
    case "Monday":
      $myresult = 1; 
      break;
    case "Tuesday":
      $myresult = 2; 
      break;
    case "Wednesday":
      $myresult = 3; 
      break;
    case "Thursday":
      $myresult = 4; 
      break;
    case "Friday":
       $myresult =  5; 
      break;
    case "Saturday":
      $myresult = 6; 
      break;
    case "Sunday":
      $myresult = 7; 
      break;
    }
    return $myresult;
  }
  
  
  function getNumDaysOfMonth($month, $year)
  {
    
    // non-leap year         
    $nonleap = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);  
    
    // leap year 
    $leap = Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);  
    
    $isLeap = false ;
    
    if (($year % 4) == 0) { 
    
      if (($year % 100) == 0 && ($year % 400) != 0) { 
        $isLeap = false;
      } 
      else { 
        $isLeap = true;
      } 
    
    }  
    else { 
    
      $isLeap = false;
    } 
    
    
    if($isLeap == true) { 
      return $leap[$month - 1];
    } 
    else { 
      return $nonleap[$month - 1]; 
    } 

  }
  
  function isToday($month,$day,$year) 
  {
    
     if( ($month == intval(date("m"))) && ( $day == intval(date("d")))
      && ($year == intval(date("Y")))){
       return true;  
     } else {
      return false;
     }  
    
  }

  //generate and output the html code for the calendar:
  function generateHTML($toUrl) 
  {   
       
      //configure top navigation
       
      $nextMonth = $this->month + 1;
      $nextYear  = $this->year;
      
      if($nextMonth == 13) {
        $nextMonth = 1;  
        $nextYear = $this->year + 1;
      }
      
      $prevMonth = $this->month - 1;
      $prevYear = $this->year;
      
      if($prevMonth == 0) {
        $prevMonth = 12;  
        $prevYear = $this->year - 1;
      }      
      
      //configure bottom navigation

      $gobackwards = $toUrl . "&m=" . $prevMonth . "&y=" . $prevYear;
      $goforward = $toUrl . "&m=" . $nextMonth . "&y=" . $nextYear;
      
      echo "<small>";
      echo "<table border=\"1\" cellpadding=\"3\">\n";
      echo "<tr>\n";
      echo "   <td colspan=\"7\">";
      echo "      <table border=\"0\" cellpadding=\"0\" width=\"100%\">\n";
      echo "      <tr>\n";
      echo "           <td width=\"5%\"><a href=\"" . $gobackwards . 
                                                            "\"><<</a></td>\n";
      echo "          <td width=\"90%\" align=\"center\"><b>" . 
      $this->getMonthName($this->month , $this->day , $this->year) . 
      " " . $this->year . "</b></td>\n";
      echo "         <td width=\"5%\"><a href=\"" . $goforward .
                                                            "\">>></a></td>\n";
      echo "      </tr>\n";
      echo "      </table>\n";
      echo "   </td>";
      echo "</tr>\n";      
      echo "<tr>\n";
      echo "   <td><b>Sun</b></td>\n";
      echo "   <td><b>Mon</b></td>\n";
      echo "   <td><b>Tue</b></td>\n";
      echo "   <td><b>Wed</b></td>\n";
      echo "   <td><b>Thu</b></td>\n";
      echo "   <td><b>Fri</b></td>\n";
      echo "   <td><b>Sat</b></td>\n";
      echo "</tr>\n";
      
      $newrow = 1;
      $monLoop = $this->getNumDaysOfMonth($this->month, $this->year);
      $startDay = intval($this->getWeekDay($this->getDayOfMonth(
                                               $this->month, 1, $this->year)));
      $begin = false;
      

      
      for($i=1; $i <= $monLoop; $i++) {
        
         if($startDay == 7) {
        $begin = true;  
       }
       
         if($newrow == 8 || $newrow == 1) {
           echo "<tr>";
           $newrow = 1;  
         }
         
         if($begin == true) {
          
          if( $this->isToday($this->month, $i, $this->year)) {
            echo "   <td><font color=\"red\">" . $i . "</font></td>"; 
          } else {
            echo "   <td>" . $i . "</td>";   
         } 
         }
         
         if($i <= $startDay && $begin == false ) {
           
           echo "   <td> </td>";
           
           if($i == $startDay) {
             $begin = true;  
             $i = 0;
           }
           
         } 
         
         $newrow++;
         
     if($newrow == 8) {
           echo "</tr>";
         }          
        
      }
      
      echo "</table>\n";
      echo "</small>";
    
  }

}



Now, In the second page we will add a method to test our calendar class.


See other Scripts in Php date and time

Submit Your Scripts:


System Message:
  • Your PHP script has been sent. Please allow 48 hours for processing.


If you would like to have your PHP scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]