PHPRO.ORG

This is a basic calendar class which displays a simple calendar in a table. No surprises there. if the date is not set, then the setToday() method defaults to today. In the example it is set to December 2004 to show possibilities. The class makes use of the xajax library to allow for refreshing of the calendar without refreshing the page.

The calander dates are generated from PHP itself so there is no need for a database such as MySQL or other to support it. This allows a wider range of portability of applications developed with it.



<?php

error_reporting
(E_ALL);

 class 
proCal {

    
/*
     * @array today
     */
    
public $today;

    
/*
     * @int seconds
     */
    
private $seconds;

    
/*
     * @int minutes
     */
    
private $minutes;

    
/*
     * @int hours
     */
    
private $hours;
    
    
/*
     * @string first day of month
     */
    
private $firstDay;

    
/*
     * @int full weeks in the month
     */
    
private $full_weeks;

    
/*
     * @string last day of month
     */
    
private $lastDay;

    private 
$actday 0;

    
/*
     * @string the previouse month
     */
    
private $prev_month;

    
/*
     * @string next month
     */
    
private $next_month;

    
/*
     * @int the unix timestamp
     */
    
private $timestamp;

    
/*
     * @int unix timestamp for next month
     */
    
private $next_month_ts;

    
/*
     * @in unix timestamp for previous month
     */
    
private $prev_month_ts;


    
/*
    * @object xajax instance
    */
    
private $xajax;

    
/*
     * @string The xajax javascript
     */
    
public $xajax_js;


    
/**
    *
    * @constructor Sets default value of today to current date
    *
    * @access public
    *
    * @return void
    *
    */
    
public function __construct()
    {
        
/*** default timestamp ***/
        
$this->setTodaytime() );

        
/*** include xajax ***/
        
include 'xajax/xajax_core/xajax.inc.php';

        
/*** and fire it up ***/
        
$this->xajax = new xajax();

        
/*** turn on debugging ***/
        // $this->xajax->setFlag('debug',true);
        /*** register xajax functions ***/
        
$this->xajax->register(XAJAX_FUNCTION, array('showPrevMonth'$this'showPrevMonth') );
        
$this->xajax->register(XAJAX_FUNCTION, array('showNextMonth'$this'showNextMonth') );
        
$this->xajax->register(XAJAX_FUNCTION, array('setDate'$this'setDate') );
        
/*** process the request ***/
        
$this->xajax->processRequest();
        
/*** assign the create javascript to a variable ***/
        
$this->xajax_js $this->xajax->getJavascript("/xajax/");
    }

    
/**
    *
    * @show the previous month
    *
    * @access private
    *
    * @param int $timestamp
    *
    * @return object
    *
    */
    
public function showPrevMonth($timestamp)
    {
        
/*** put the date and time info into an array ***/
        
$this->setToday($timestamp);
        
/*** a new response object ***/
        
$ores = new xajaxResponse();
        
/*** put the calandar into the response object ***/
        
$ores->assign('phpro_cal''innerHTML'$this->showCalander());
        return 
$ores;
    }


    public function 
setDate($date)
    {
        
$ores = new xajaxResponse();
        
$ores->assign('calTime''innerHTML'$date);
        return 
$ores;
    }


    
/**
    *
    * @show the next month
    *
    * @access private
    *
    * @param int $timestamp
    *
    * @return object
    *
    */
    
public function showNextMonth($timestamp)
    {
        
$ores = new xajaxResponse();
        
$this->setToday($timestamp);
        
$ores->assign('phpro_cal''innerHTML'$this->showCalander());
        return 
$ores;
    }




    
/**
     *
     * @set today
     *
     * @access public
     *
     * @param int $today (the current date UNIX TIMESTAMP)
     *
     * @return void
     *
     */
    
public function setToday($today=null)
    {
        
$this->timestamp $today;
        
$this->today getDate($today);
        
/*** set the timestamp for next next month ***/
        
$this->next_month_ts strtotime('+1 month'$this->today[0]);
        
/*** set the timestamp for last month ***/
        
$this->prev_month_ts strtotime('-1 month'$this->today[0]);

        
$this->setFirstDay();
        
$this->setLastDay();
        
$this->setPrevMonth();
        
$this->setNextMonth();
    }



    
/*
    * 
    * @set the first day of the month
    *
    * @access private
    *
    * @return void
    *
    */ 
    
private function setFirstDay()
    {
        
$this->firstDay getdate(mktime(0,0,0,$this->today['mon'],1,$this->today['year']));
    }

    
/*
    *
    * @set the last day of the month
    *
    * @access private
    *
    * @return void
    *
    */
    
private function setLastDay()
    {
        
$this->lastDay getdate(mktime(0,0,0,$this->today['mon']+1,0,$this->today['year']));
    }

    
/*
    *
    * @set the previous month
    *
    * @access private
    *
    * @return void
    *
    */
    
private function setPrevMonth()
    {
        
$this->prev_month date('M'strtotime("-1 month"$this->timestamp));
    }


    
/**
    *
    * @set the next month
    *
    * @access private
    *
    * @return void
    *
    */
    
private function setNextMonth()
    {
        
$this->next_month date('M'strtotime("+1 month"$this->timestamp));
    }


    
/**
    *
    * @the calendar table head
    *
    * @access private
    *
    * @return string
    *
    */
    
private function tableHead()
    {
        
$this->setPrevMonth();
        
$th '<form id="proCal">
            <table>
            <thead><tr><th colspan="7">'
.$this->today['year'].'</th></tr></thead>
            <tfoot><tr><th colspan="7"><input type="input" id="calTime" name="calTime" value="'
.$this->createTimestamp().'" /></th></tr></tfoot>
            <tbody>
            <tr><td colspan="2"><span onClick="xajax_showPrevMonth(\''
.$this->prev_month_ts.'\')">'.$this->prev_month.'<span></td><th colspan="3">'.$this->today['month'].'</th><td colspan=2"><span onClick="xajax_showNextMonth(\''.$this->next_month_ts.'\')">'.$this->next_month.'<span></td></tr>'."\n".'
            <tr class="week_days">
            <td>Su</td><td>Mo</td><td>Tu</td><td>We</td><td>Th</td>
            <td>Fr</td><td>Sa</td></tr>'
."\n";
        return 
$th;
    }


    
/**
    *
    * @the first row of the calendar
    *
    * @access private
    *
    * @return string
    *
    */
    
private function firstRow()
    {
        
// Display the first calendar row with correct positioning
        
$fr '<tr>';
        
/*** empty table cells to begin with ***/
        
for($i=0$i<$this->firstDay['wday']; $i++)
        {
            
$fr .= '<td>&nbsp;</td>';
        }

        
/*** show the rest of the first row ***/
        /*** we use 6 instead of seven because sunday is zero ***/
        
for($i=$this->firstDay['wday'];$i<=6;$i++)
        {
            
$this->actday++;
            if (
$this->actday == $this->today['mday'])
            {
                
$class ' class="current_day"';
            }
            else
            {
                
$class '';
            }
            
$fr .= '<td'.$class.'  onclick="xajaxSetDate('.$this->actday.')">'.$this->actday.'</td>';
        }
        
$fr .= '</tr>'."\n";
        return 
$fr;
    }


    
/**
    *
    * @number of full weeks in the month
    *
    * @access private
    *
    * @return int
    *
    */
    
private function getFullWeeks()
    {
        
/*** Get how many full weeks are in the month ***/
        
return floor(($this->lastDay['mday']-$this->actday)/7);
    }


    
/**
    *
    * the row of weeks
    *
    * @access private
    *
    * @return string
    *
    */
    
private function weekRows()
    {
        
$this->full_weeks $this->getFullWeeks();

        
$wr '';
        for (
$i=0;$i<$this->full_weeks;$i++)
        {
            
$wr .= '<tr>';
            for (
$j=0;$j<7;$j++)
            {
                
$this->actday++;
                if (
$this->actday == $this->today['mday'])
                {
                    
$class ' class="current_day"';
                }
                else
                {
                    
$class '';
                }
                
$wr .= '<td'.$class.'  onclick="xajaxSetDate('.$this->actday.')">'.$this->actday.'</td>'
            }
            
$wr .= '</tr>'."\n";
        }
        return 
$wr;
    }


    
/**
    *
    * @show the calendar
    *
    * @access public
    *
    * @return string
    *
    */
    
public function showCalander()
    {

        
/*** the table head ***/
        
$cal  $this->tableHead();
        
$cal .= $this->firstRow();
         
$cal .= $this->weekRows(); 
        
$cal .= $this->endRows();
        return 
$cal;
    }

    
/**
    *
    * @the end rows of the calendar
    *
    * @access private
    *
    * @return string
    *
    */
    
private function endRows()
    {
        
/*** the end row variable ***/
        
$er '';
        
//Now display the rest of the month
        
if ($this->actday $this->lastDay['mday'])
        {
            
$er .= '<tr>';
      
            for (
$i=0$i<7;$i++)
            {
                
$this->actday++;
                if (
$this->actday == $this->today['mday'])
                {
                    
$class ' class="current_day"';
                }
                else
                {
                    
$class '';
                }
                if (
$this->actday <= $this->lastDay['mday'])
                {
                    
$er .= '<td'.$class.'  onclick="xajax_SetDate('.$this->actday.')">'.$this->actday.'</td>';
                }
                else
                {
                    
$er .= '<td>&nbsp;</td>';
                }
            }
            
$er .= '</tr>'."\n";
        }
        
/*** end the table body and table ***/ 
        
$er .=  '</tbody></table></form>';
        return 
$er;
    }


    public function 
createTimestamp()
    {
        
$time str_pad($this->today['hours'], 20STR_PAD_LEFT).':'.str_pad($this->today['minutes'], 20STR_PAD_LEFT).':00';
        
$date $this->today['year'].'-'.str_pad($this->today['mon'], 20STR_PAD_LEFT).'-'.str_pad($this->today['mday'], 20STR_PAD_LEFT);
        return 
$date.' '.$time;

    }
/*** end of class ***/

$cal = new proCal;
// $cal->setToday(1102229445);
?>

<html>
<head>
<title>PHPRO.ORG Calander</title>

<?php echo $cal->xajax_js?>
<style type="text/css">
.current_day {
    background-color: #ff8000;
}

.current_year {
    background-color: #ffffff;
}

.week_days {
    background-color: #ff8000;
    margin:0px;
}


thead, tfoot {
    background-color: #800000;
    color: #ffffff;
    text-align:center;
}

</style>

</head>
<body>

<div id="phpro_cal">
<?php echo $cal->showCalander(); ?>
</div>

</body>
</html>