Jump to content

Calendar


aftabn10

Recommended Posts

Thanks to Angela Bradley, I have managed to get a script to display a monthly calendar. What I would like to know is how i could add a previous and next button to display other months of the calendar. The following is the script I have used:

 

<?php 
//This gets today's date 
$date =time () ; 

//This puts the day, month, and year in seperate variables 
$day = date('d', $date) ; 
$month = date('m', $date) ; 
$year = date('Y', $date) ;

//Here we generate the first day of the month 
$first_day = mktime(0,0,0,$month, 1, $year) ; 

//This gets us the month name 
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day) ; 

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){ 
case "Sun": $blank = 0; break; 
case "Mon": $blank = 1; break; 
case "Tue": $blank = 2; break; 
case "Wed": $blank = 3; break; 
case "Thu": $blank = 4; break; 
case "Fri": $blank = 5; break; 
case "Sat": $blank = 6; break; 
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 

//Here we start building the table heads 
echo "<table border=0 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";

//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 ) 
{ 
echo "<td></td>"; 
$blank = $blank-1; 
$day_count++;
} 

//sets the first day of the month to 1 
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) 
{ 
echo "<td> $day_num </td>"; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td> </td>"; 
$day_count++; 
} 

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

 

Please could somebody help.

 

Thanks in advance.

Link to comment
Share on other sites

add this code:

 

// assume this month
$month = date('m', $date);

// change if a valid month has been supplied
if (isset($_POST['month']) && !empty($_POST['month']) && is_numeric($_POST['month'])) {
    $postMonth = (int) $_POST['month'];
    if ($postMonth >= 1 && $postMonth <= 12) {
        $month = $postMonth;
    } else if ($postMonth > 12) {
        $month = 1;
    } else if ($postMonth < 1) {
        $month = 12;
    }
    unset($postMonth);
}

 

probably you want to add this functionality to your submit buttons:

 

<?php
function monthCarousel($currentMonth) {
    if ($currentMonth >= 1 && $currentMonth <= 12) {
        return $currentMonth;
    } else if ($currentMonth < 1) {
        return 12;
    } else if ($currentMonth > 12) {
        return 1;
    }
}
?>

<input type="submit" name="previous" id="previous" value="<?php print monthCarousel($month - 1); ?>" />
<input type="submit" name="next" id="next" value="<?php print monthCarousel($month + 1); ?>" />

Link to comment
Share on other sites

ignace, silly question but just want to clarify whereabouts to add the code. I am guessing the function code would go towards the top and the buttons code more towards the following:

 

//Here we start building the table heads 
echo "<table border=0 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";

 

Would that be correct?

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.