Zend - The PHP Company




Date & Time

Add Code


Smarty Enabled Calendar  

Type: application
Added by: bmickler
Entered: 01/09/2006
Last modified: 31/10/2005
Rating: - (fewer than 3 votes)
Views: 5354
Small calendar that displays the days, month, and abbrev. year. Allows the user to move forward and backward one month or one year, and quickly return to today's view.


<?php
/**
 * Project:  Smarty Enabled Calendar
 *
 * File:  Calendar.php
 *
 * Based on 'kghoker at yahoo.com's tiny calendar (see comments below).
 *
 * This code renders a cool-looking, small calendar 150 px wide with the
 * following features:
 *
 *  -  Clean UI
 *  -  Move forward and backward one month
 *  -  Move forward and backward one year
 *  -  Easily return to today's view
 *  -  Utilises Smarty
 *
 * There are two files contained in this snippett; the php code-behind page and
 * the Smarty template page.  Use of this code presumes you have PHP and Smarty
 * working on your system.
 *
 * To install, copy the code into their two respective files.  I name the code-
 * behind page 'calendar.php' and the template page 'calendar.tpl'.  Make sure
 * you include the smarty.class.php or use some other way to instantiate the
 * Smarty object.  Have fun!
 *
 * Bryce
 * http://www.brycemickler.com/
 *
 * ---
 * original comments
 * ---
 *
 * mail:  kghoker at yahoo.com  9/17/2001
 * Much like "A Tiny Calendar", just less code.
 * Give it a month/day/year, and it will generate the calendar and highlight the day.
 * Colors are customizeable.  If no date arguments given, assumes today.
 *
 */

/**
 * Smarty Configuration stuff
 *
 */
require_once('smarty.class.php');
$smarty = new Smarty();

define('SCRIPT_NAME',basename(__FILE__));

/**
 * Grab form arguments from the URL if there are any
 */
if(isset($_GET['m'])){
    if(
is_numeric($_GET['m']) && (strlen($_GET['m']) <= 2) && ($_GET['m'] < 13)){
        
$newMonth strip_tags(trim($_GET['m']));
    }
}
if(isset(
$_GET['y'])){
    if(
is_numeric($_GET['y']) && (strlen($_GET['m']) <= 4)){
        
$newYear strip_tags(trim($_GET['y']));
    }
}

$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$monthAbrevs = array(1=>'Jan.',2=>'Feb.',3=>'Mar.',4=>'Apr.',5=>'May',6=>'June',
                     
7=>'July',8=>'Aug.',9=>'Sept',10=>'Oct.',11=>'Nov.',12=>'Dec.');

 
/**
 * Calculate the calendar data
 */
$monthNum = (isset($newMonth)) ? $newMonth date("n",time());
$fullYear = (isset($newYear)) ? $newYear date("Y",time()); //used in href links
$abrevYear substr($fullYear,2,2);
$today date("j"time());

$dayOne date("w",mktime(1,1,1,$monthNum,1,$fullYear));
$numDays date("t",mktime(1,1,1,$monthNum,1,$fullYear));
$monthName $monthAbrevs[$monthNum] . ' '' . $abrevYear;


/**
 * It is very difficult to use smarty to solve complex problems such as computing
 * where days fall in a calendar view.  On the other hand, Smarty really does a
 * good job of displaying arrays so we are going to create an array that holds
 * both blank days and normal days. Smarty will loop through this array and
 * display the values in the calendar.
 *
 * Blank days are days that belong to the previous month, but are still in the
 * calendar week of the first day of the current month.  For example;  Sept. 1,
 * 2006 is on a Friday.  There are 5 blank days that belong to the previous month
 * (Sunday through Thursday).  We need to pad the array with 5 blank days.
 */

/**
 * How many blank days?
 */
$blank_days = array();
for($i = 0; $i < $dayOne; $i++){
    $blank_days[$i] = '';
}
$num_blank_days = count($blank_days);

/**
 * Create the numdays_array and pad it with the blank days.
 */
$count = 1;
for($i = 1;$i <= $num_blank_days; $i++){
    $numdays_array[$count] = '';
    $count++;
}

/**
 * Now add the rest of the days of the month to the array.  The value will be the
 * day of the month.  Blank days have a value of ''.
 */
for($i = 1; $i <= $numDays; $i++){
    $numdays_array[] = $i;
}

/**
 * Create the URL strings to move back and forth in time.
 */

//  Calculate the previous month & its' 
year
$prevMonthsNum 
= ($monthNum == 1) ? 12 $monthNum 1;
$prevMonthsYear = ($prevMonthsNum == 1) ? $fullYear $fullYear;
$prevMonthString SCRIPT_NAME "?m=$prevMonthsNum&y=$prevMonthsYear";

//  Calculate the next month & its' year
$nextMonthsNum = ($monthNum == 12) ? $monthNum 1;
$nextMonthsYear = ($nextMonthsNum == 1) ? $fullYear $fullYear;
$nextMonthString SCRIPT_NAME "?m=$nextMonthsNum&y=$nextMonthsYear";

//  Go back one full year
$lastYear $fullYear 1;
$lastYearString SCRIPT_NAME "?m=$monthNum&y=$lastYear";

//  Go ahead one full year
$nextYear $fullYear 1;
$nextYearString SCRIPT_NAME "?m=$monthNum&y=$nextYear";


/**
 * send variables to Smarty
 */
$smarty->assign('MONTHNAME',$monthName);
$smarty->assign('TODAY',$today);
$smarty->assign('NUMDAYS_ARRAY',$numdays_array);
$smarty->assign('WEEKDAYS',$weekdays);
$smarty->assign('PREV_MONTH_LINK',$prevMonthString);
$smarty->assign('NEXT_MONTH_LINK',$nextMonthString);
$smarty->assign('LAST_YEAR_LINK',$lastYearString);
$smarty->assign('NEXT_YEAR_LINK',$nextYearString);

/**
 * Display the page
 */
$smarty->display('calendar.tpl');

/**
 * End of 'calendar.php'
 */
?>

<!-- Begin 'calendar.tpl' Smarty template file.  Don't include this comment line //-->

{ * Smarty * }

<html>
<head>
{literal}
<style type="text/css">
    a { text-decoration: none}
</style>
{/literal}
<title>Calendar</title>
</head>
<body><table border="0" cellpadding="0" cellspacing="0" width="150">
    <tr>
        <td align="center">
            <a href="{$LAST_YEAR_LINK}"><<</a>
        </td>
        <td align="center">
            <a href="{$PREV_MONTH_LINK}"><</a>
        </td>
        <td bgcolor="#ffffff" colspan="3" valign="middle" align="center">
            <font color="#333399" face="arial" size="3">
            <b>
            {$MONTHNAME}
            </b></font>
        </td>
        <td align="center">
            <a href="{$NEXT_MONTH_LINK}">></a>
        </td>
        <td align="center">
            <a href="{$NEXT_YEAR_LINK}">>></a>
        </td>
    </tr>
    <tr>

    {foreach from=$WEEKDAYS item=WEEKDAY}
        <td bgcolor="#FFFFFF" valign="middle" align="center" width="15%">
            <font color="#000000" face="arial" size="1"><b>{$WEEKDAY}</b></font>
        </td>
    {/foreach}

    </tr>
    <tr>

    {foreach from=$NUMDAYS_ARRAY key=KEY item=CURR_DAY}

        {if $CURR_DAY == ""}
            <td bgcolor="#ffffff" width="15%">
                <font color="#ffffff" face="arial" size="1">&nbsp;</font>
            </td>
        {elseif $CURR_DAY == $TODAY}
            <td bgcolor="#CC0000" valign="middle" align="center" width="15%">
                <font color="#FFFFFF" face="arial" size="1">
                {$CURR_DAY}
                </font>
            </td>
        {else}
            <td bgcolor="#FFFFFF" valign="middle" align="center" width="15%">
                <font color="#000000" face="arial" size="1">
                {$CURR_DAY}
                </font>
            </td>
        {/if}

        {if $KEY is div by 7}
            </tr><tr>
        {/if}

    {/foreach}

    </tr>
    <tr>
        <td colspan="7" align="left">
            <font color="#000000" face="arial" size="1">
                <a href="calendar.php">today</a>
            </font>
        </td>
    </tr>
</table>
</body>
</html>


Usage Example




Rate This Script





Search



This Category All Categories