Jump to content

calander code semi working


blueman378

Recommended Posts

hi guys well ive got a calender code im modifying to add a next month/previous feature,

so heres the code

<?php
$next = $_GET["next"];
$back = $_GET["back"];
$max = "12";
$min = "1";
    // get this month and this years as an int
    	if (isset($next)) {
	$next++;
    $thismonth = $next;
} elseif (isset($back)) {
    $back--;
    $thismonth = $back;
} else {
    $thismonth = ( int ) date( "m" );
}
    $thisyear = date( "Y" );
    
    // find out the number of days in the month
    $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );
    
    // create a calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );
    
    // get the start day as an int (0 = Sunday, 1 = Monday, etc)
    $startday = jddayofweek( $jd , 0 );
    
    // get the month as a name
    // $monthname = jdmonthname( $jd, 1 )
if ($thismonth == "1") {
    $monthname = "January";
} elseif ($thismonth == "2") {
    $monthname = "February";
}
elseif ($thismonth == "3") {
    $monthname = "March";
}
elseif ($thismonth == "4") {
    $monthname = "April";
}
elseif ($thismonth == "5") {
    $monthname = "May";
}
elseif ($thismonth == "6") {
    $monthname = "June";
}
elseif ($thismonth == "7") {
    $monthname = "July";
}
elseif ($thismonth == "8") {
    $monthname = "August";
}
elseif ($thismonth == "9") {
    $monthname = "September";
}
elseif ($thismonth == "10") {
    $monthname = "October";
}
elseif ($thismonth == "11") {
    $monthname = "November";
}
elseif ($thismonth == "12") {
    $monthname = "December";
}
else {
    echo "Unknown month";
}
?>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>S</strong></td>
        <td><strong>M</strong></td>
        <td><strong>T</strong></td>
        <td><strong>W</strong></td>
        <td><strong>T</strong></td>
        <td><strong>F</strong></td>
        <td><strong>S</strong></td>
    </tr>
    <tr>
<?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {
    
        echo "\t\t<td>-</td>\n";
        $emptycells ++;
    
    }
    
    // renders the days
    
    $rowcounter = $emptycells;
    $numinrow = 7;
    
    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    
        $rowcounter ++;
        
        echo "\t\t<td>$counter</td>\n";
        
        if( $rowcounter % $numinrow == 0 ) {
        
            echo "\t</tr>\n";
            
            if( $counter < $numdaysinmonth ) {
            
                echo "\t<tr>\n";
            
            }
        
            $rowcounter = 0;
        
        }
    
    }
    
    // clean up
    $numcellsleft = $numinrow - $rowcounter;
    
    if( $numcellsleft != $numinrow ) {
    
        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        
            echo "\t\t<td>-</td>\n";
            $emptycells ++;
        
        }
    
    }

?>
    </tr>
<tr>
<td colspan="3"><form action="<?php $_server['php_self']; ?>">
<input type="hidden" name="back" value="<?php echo "$thismonth"; ?>" />
<?php
if ($thismonth > $min){
    echo '<input type="submit" value="Back" />';
}
?>
</form>
</td><td><?php echo "$thismonth";?></td><td colspan="3">
<form action="<?php $_server['php_self']; ?>">
<input type="hidden" name="next" value="<?php echo "$thismonth"; ?>"
<?php
if ($thismonth < $max){
    echo '<input type="submit" value="Next" />';
}
?>
</form></td>
</tr>
</table>

 

which is working ok on first run,

however when you click next/ back the day the month starts on stays the same,

 

eg

this month is march so the calander looks like this

(first image)

but when i click back (february)

i get (second image)

 

any ideas?

 

cheers

 

 

Link to comment
Share on other sites

well i believe i must modify this line:

 // create a calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );

i think i need to define the "m" as whatever month it is eg something like this

 // create a calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, date( "$thismonth" ),date( 1 ), date( "Y" ) );

 

jsut dabling not to sure help appreciated (so is code optimization :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.