Thursday, December 15, 2011

calendar using php


<?

function calendar($m,$y)
{

$daysInMonth = date("t",mktime(0,0,0,$m,1,$y));
$firstDay = date("w", mktime(0,0,0,$m,1,$y));
$tempDays = $firstDay + $daysInMonth;
$weeksInMonth = ceil($tempDays/7);
$counter = 0;
$week = array();

  for($j=0;$j<$weeksInMonth;$j++) {
            for($i=0;$i<7;$i++) {
                $counter++;
                $week[$j][$i] = $counter;
                // offset the days
                $week[$j][$i] -= $firstDay;
                if (($week[$j][$i] < 1) || ($week[$j][$i] > $daysInMonth)) {  
                    $week[$j][$i] = "";
                }
            }
        }
$week = array('ymon'=>date('M', mktime(0,0,0,$m,1,$y)).' '.$y,'c'=>$week,'nm'=>date('F',mktime(0,0,0,$m+1,1,$y)),'m'=>$m,'y'=>$y);
//echo '<pre>'.print_r($week).'</pre>';exit;
return $week;
}


?>
save above code as cal.php:


<? $week =calendar(date('n'),date('Y')); ?>
<div >

<div style="padding:1px 0px 5px 0px; " ><span style="background-color:#0066CC; border:1px solid #666666; color:#FFFFFF; padding:2px; font-weight:bold;"><?=$week['ymon'];?></span>

<span style=" background-color:#0066CC; border:1px solid #666666; color:#FFFFFF; padding:2px; margin-left:70px; cursor:default;" onclick="nxtmonth(<?=$week['m'] ?>,<?=$week['y'] ?>);"><?=$week['nm'] ?>>></span>

</div>

<table width="100" border="1" cellpadding="2" cellspacing="2">



<tr>

<th>Sun</th>

<th>Mon</th>

<th>Tue</th>

<th>Wed</th>

<th>Thur</th>

<th>Fri</th>

<th>Sat</th>

</tr>
<?php

foreach ($week['c'] as $key => $val) {



echo "<tr>";

for ($i=0;$i<7;$i++) {

echo "<td align='center'>$val[$i]</td>";

}

echo "</tr>";

}

?>

</table>

</div>
For Next Month:

function nextMonth()
{
$m = $this->input->post('m');
$y = $this->input->post('y');
if($m==12){$y +=1; $m=1;}else {$m +=1;}

$week = calendar($m,$y);

}


Bind Some Event to Element and trigger that function when click the element

Scenario: Suppose if we used same click event function in various web pages. if you want do some logic some page button for that need to re...