The Most Active and Friendliest
Affiliate Marketing Community Online!

“Adavice”/  “1Win

Tip : display calendar using php on your site

D

dman_2007

Guest
Here's the php code you can use to display a calendar on your site. By default, the calendar displays current server date, but user can navigate to a different date easily by changing month and year value. You can use this code anywhere you want and for any purpose you want, no restrictions. Enjoy!

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>PHP Calendar</title>
  <meta name="GENERATOR" content="Quanta Plus" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
    body {
      text-align: center;
      width:      100%;
      font:       9pt Arial;
    } 
    
    a:link {
      text-decoration: none; 
      color:           #5F87FF;
    }
    
    a:visited {
      text-decoration: none;   
      color:           #5F87FF;  
    }

    .etable {
      margin-left:  auto;
      margin-right: auto;
      border:       1px solid #CDCDCD;   
    }

    .etable_header {
      background-color: #A5A5A5;
    }

    .etable td {
      background-color: #DCF9FF;
      padding:          2px;
      text-align:       center;
    }

    #curr_day1 {
      background-color: #7B9AFF;
    }

    #curr_day2 {
      background-color: #7B9AFF; 
    }
    
    .disabled_link {
      color: #D7D7D7;  
    }
     
  </style>
</head>
<body>
  <div class="calendar">
    <?php
         if(array_key_exists('custom', $_GET))
         {
           if(!array_key_exists('day', $_GET) || $_GET['day'] < 1 || $_GET['day'] > 31)
           {
             $show_error = 1;
           }
           else if(!array_key_exists('month', $_GET) || $_GET['month'] < 1 || $_GET['month'] > 12)
           {
             $show_error = 1;
           }
           else if(!array_key_exists('year', $_GET) || $_GET['year'] < 1)
           {
             $show_error = 1;
           }
           else
           {
             $temp_month_days = cal_days_in_month(CAL_GREGORIAN, $_GET['month'], $_GET['year']);
             if($_GET['day'] > $temp_month_days)
             {
               $_GET['day'] = $temp_month_days;
             }
 
             if(!($utime = mktime(0, 0, 0, $_GET['month'], $_GET['day'], $_GET['year'])))
             {
               $show_error = 1;
             } 
             else
             {
               $today = getdate($utime);
             }
           }
         }
         else
         {
           $today = getdate();
         }
          
         if(!isset($show_error))
         {
           $num_days_in_month = cal_days_in_month(CAL_GREGORIAN, $today['mon'], $today['year']);

           if($today['mday'] == 1)
           {
             $month_start_day = $today['wday'];
           }
           else
           {
             $temp_i = $today['mday'];
             $temp_j = $today['wday'];
             for(;$temp_i > 1;$temp_j--,$temp_i--)
             {
               if($temp_j == 0)
               {
                 $temp_j = 7;
               }
             }
             $month_start_day =($temp_j == 7 ? 0 : $temp_j);
           }
         } 
    ?>
    <?php 
      if(isset($show_error)) 
      { 
        echo 'Error! Invalid arguments provided';
      } 
      else 
      { 
    ?>
      <table class="etable">
        <tr class="etable_header">
          <th>
            <a href="calendar.php?custom=1&amp;year=<?php echo ($today['year'] - 1); ?>&amp;month=<?php echo $today['mon']; ?>&amp;day=<?php echo $today['mday']; ?>">&lt;</a>
          </th>
          <th colspan="5"><?php echo $today['year']; ?></th>
          <th>
            <a href="calendar.php?custom=1&amp;year=<?php echo ($today['year'] + 1); ?>&amp;month=<?php echo $today['mon']; ?>&amp;day=<?php echo $today['mday']; ?>">&gt;</a>
          </th> 
        </tr>
        <tr class="etable_header">
          <th>
            <?php if($today['mon'] == 1) { ?>
              <span class="disabled_link">&lt;</span>
            <?php } else  { ?>
              <a href="calendar.php?custom=1&amp;year=<?php echo $today['year']; ?>&amp;month=<?php echo ($today['mon'] - 1); ?>&amp;day=<?php echo $today['mday']; ?>">&lt;</a>
            <?php } ?>
          </th>
          <th colspan="5"><?php echo $today['month']; ?></th>
          <th>
            <?php if($today['mon'] == 12) { ?>
              <span class="disabled_link">&gt;</span>
            <?php } else  { ?>
              <a href="calendar.php?custom=1&amp;year=<?php echo $today['year']; ?>&amp;month=<?php echo ($today['mon'] + 1); ?>&amp;day=<?php echo $today['mday']; ?>">&gt;</a>
            <?php } ?>
          </th>
        </tr>
        <tr class="etable_header">
          <th <?php if($today['wday'] == 0) { echo 'id="curr_day1"'; } ?>>S</th>
          <th <?php if($today['wday'] == 1) { echo 'id="curr_day1"'; } ?>>M</th>
          <th <?php if($today['wday'] == 2) { echo 'id="curr_day1"'; } ?>>T</th>
          <th <?php if($today['wday'] == 3) { echo 'id="curr_day1"'; } ?>>W</th>
          <th <?php if($today['wday'] == 4) { echo 'id="curr_day1"'; } ?>>T</th>
          <th <?php if($today['wday'] == 5) { echo 'id="curr_day1"'; } ?>>F</th>
          <th <?php if($today['wday'] == 6) { echo 'id="curr_day1"'; } ?>>S</th>
        </tr>
        <?php
          for($temp_j = 0,$temp_i = 1; $temp_i <= $num_days_in_month;$temp_j++)
          {
            if($temp_j == 0)
            {
              echo '<tr>';
            }
            if($temp_i == 1 && $temp_j != $month_start_day)
            {
              echo '<td>&nbsp;</td>';
            }
            else if($temp_i == 1 && $temp_j == $month_start_day)
            {
              if($today['mday'] == 1)
              {
                echo "<td id=\"curr_day2\">1</td>";
              }
              else
              {
                echo "<td>1</td>";
              }
              ++$temp_i;
            }
            else
            {
              if($today['mday'] == $temp_i)
              {
                echo "<td id=\"curr_day2\">$temp_i</td>";
              }
              else
              {
                echo "<td>$temp_i</td>";
              }
              ++$temp_i;
            }
                                     
            if($temp_j == 6)
            {
              echo '</tr>';
              $temp_j = -1;
            }
          }
                                                   
          if($temp_j != -1)
          {
            echo '</tr>';
          }
        ?>
      </table>
    <?php } ?>
  </div>
</body>
</html>

A demo of this script can be seen in action here : http://demo.ukwebmasterworld.com/php-calendar/calendar.php
 

Attachments

  • calendar.php.zip
    1.6 KB · Views: 47
Last edited:
banners
Back