Wednesday 18 June 2014

Calender in php

Calender in php, codeigniter

<?
if($_POST)
{
    $day=01;
    $month=$_POST['month'];
    $year=$_POST['year'];
}
else
{
    $day=date('d');
    $month=date('m');
    $year=date('Y');
}

$prev_month=$month-1;
$next_month=$month+1;

$prev_year=$year;
$next_year=$year;

if($prev_month==0)
{
    $prev_month=12;
    $prev_year=$year-1;
}
if($next_month==13)
{
    $next_month=1;
    $next_year=$year+1;
}

$days_to_add=date('N',strtotime(date('l',strtotime($day.'-'.$month.'-'.$year))));

($days_to_add==7) ? $days_to_add=0 : '';

$num_days=date('t',strtotime($day.'-'.$month.'-'.$year));

$weeks=ceil(($num_days+$days_to_add)/7);

$days=array(1=>'Mon',2=>'Tue',3=>'Wed',4=>'Thu',5=>'Fri',6=>'Sat',7=>'Sun');

$date_to_display=1;
echo "<form action='' method='post'> <input type='text' name='month' value='".$prev_month."' hidden/><input type='text' name='year' value='".$prev_year."' hidden/><input type='submit' name='next' value='Prev'/></form> ... <form action='' method='post'> <input type='text' name='month' value='".$next_month."' hidden/><input type='text' name='year' value='".$next_year."' hidden/><input type='submit' name='next' value='Next'/> </form>";

echo "<br><br>Calender for ".date('M',strtotime($day.'-'.$month.'-'.$year))." ,$year<br><br>";
echo "<table cellpadding='3'>";
echo "<tr>";
for($i=1;$i<=7;$i++)
{
echo "<td>".$days[$i]."</td>";
}
echo "<tr>";

for($i=1;$i<=$weeks;$i++)
{
    echo "<tr>";

        for($j=1;$j<=7 && $date_to_display<=$num_days;$j++)
        {
            if($i==1)
            {
                if($days_to_add>1)
                {
                    echo "<td></td>";
                    $days_to_add--;
                }
                else
                {
                    echo "<td>$date_to_display</td>";
                    $date_to_display++;
                }
            }
            else
            {
                echo "<td>$date_to_display</td>";
                $date_to_display++;
            }
        }

    echo "</tr>";
}
echo "</table>";

?>

No comments:

Post a Comment