Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs
 
 

PHP Forum PHP related discussion, help, tips and tutorials.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 06:40 PM
dman_2007
Guest
 
Posts: n/a
Default Tip : display calendar using php on your site

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...r/calendar.php
Attached Files
File Type: zip calendar.php.zip (1.6 KB, 0 views)

Last edited by dman_2007; 04-04-2008 at 01:33 PM.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Webmaster Resources
UK WW SEO Tools
Find UK Hosts
 
The Forum Rules
Forum Rules - MUST READ
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
ad exchange affiliates asbestos cancer audio ads bid bidding directory bid directory bid directory list bid for position business clothes contest cricket writer dedicated servers delisted designer discount ecommerce executive suites finance forum global business directory google handbags internet and marketing iphone jewelry link exchange link submissions niche nokia n95 of mesothelioma paid blogging phones electronics pr7 directory replica samsung seo seo company social bookmark startup sunglasses technology uk property writer wallets web web design web development website website development website promotion wholesale yahoo backlink

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:50 PM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0