How to Insert a Calendar in Your Website Using PHP

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Insert a Calendar in Your Website Using PHP
Use a pre-made PHP calendar script or create your own. (calendar with bow image by Photoeyes from Fotolia.com)

Calendars can be used for a variety of reasons on a website: adding a calendar as a date picker for a Web form; broadcasting upcoming events in calendar form to inform your visitors; or having a decorative calendar as an extra benefit for your visitors to check the date. No matter what the purpose, it is easy to incorporate a calendar in other PHP coding on the site. There are even pre-made PHP calendars to make the process easy.

Skill level:
Moderately Easy

Other People Are Reading

Instructions

    Dreamweaver Visual Calendar

  1. 1

    Purchase and download the Dreamweaver Visual Calendar plug-in from the Adobe website. Double-click the downloaded file to install the plug-in within Dreamweaver.

  2. 2

    Open Dreamweaver and the PHP file that you want to include the calendar in.

  3. 3

    Click on "Menu," "Commands" and then "Insert Visual Calendar" to insert the calendar in the page. Save and upload the page to your Web server along with the "TDE_VCalendar" folder found in the download.

  4. 4

    Go to the admin page for the calendar. The Web address will be "www.yousite.com/TDE_VCalendar/Admin." Set the language, username and password for the calendar.

  5. 5

    Click on the "Add" button under the calendar to add a new note to the calendar. Click on "Edit" to edit the look and feel of the calendar or to delete a note.

    Coding a PHP Calendar

  1. 1

    Open a new blank page in a text editor or Web authoring application.

  2. 2

    Type in the following code to create the calendar:

    "<html>

    <body>

    <?php

    $month_Names = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");

    if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

    $Current_Month = $_REQUEST["month"];

    $Current_Year = $_REQUEST["year"];

    $prev_year = $Current_Year;

    $next_year = $Current_Year;

    $prev_month = $Current_Month-1;

    $next_month = $Current_Month+1;

    if ($prev_month == 0 ) {

    $prev_month = 12;

    $prev_year = $Current_Year - 1;

    }

    if ($next_month == 13 ) {

    $next_month = 1;

    $next_year = $Current_Year + 1;

    }

    ?>

    <table width="200">

    <tr align="center">

    <td bgcolor="#999999" style="color:#FFFFFF">

    <table width="100%" border="0" cellspacing="0" cellpadding="0">

    <tr>

    <td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>

    <td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>  </td>

    </tr>

    </table>

    </td>

    </tr>

    <tr>

    <td align="center">

    <table width="100%" border="0" cellpadding="2" cellspacing="2">

    <tr align="center">

    <td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $month_Names[$Current_Month-1].' '.$Current_Year; ?></strong></td>

    </tr>

    <tr>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Sun</strong></td>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Mon</strong></td>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Tue</strong></td>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Wed</strong></td>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Thu</strong></td>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Fri</strong></td>

    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Sat</strong></td>

    </tr>

    <?php

    $timestamp = mktime(0,0,0,$Current_Month,1,$Current_Year);

    $maxday = date("t",$timestamp);

    $thismonth = getdate ($timestamp);

    $startday = $thismonth['wday'];

    for ($i=0; $i<($maxday+$startday); $i++) {

    if(($i % 7) == 0 ) echo "<tr>\n";

    if($i < $startday) echo "<td></td>\n";

    else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>\n";

    if(($i % 7) == 6 ) echo "</tr>\n";

    }

    ?>

    </table>

    </td>

    </tr>

    </table>

    </body>

    </html>"

    Type in the code without the quotations on the first and last line. The code will generate a monthly calendar for the current month.

  3. 3

    Save the file as a PHP file type and upload it to your Web server.

Tips and warnings

  • For more PHP examples on how to do different things with a PHP calendar, see the Keith Devens link in the Reference section.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.