Jump to content

Do-it-yourself calendar. Need help moving back/forth month/year


jb60606

Recommended Posts

I've put together a very simple calendar with the following code. The calendar itself works fine, but I would like to give the user the option to, at least, click through the months (what good is a single month calendar).

 

I'm somewhat new to PHP. I can get it to move forward or backward a month or year with some manipulation of the strtotime function or by adding/subtracting thousands of seconds from the date() function - but that's it. And no matter what month I'm on, it can only move to the two adjacent months.

 

Also, if the month is currently April and I click the button to move forward one month, it takes me to May as it should. From May, if I want to move back one month it takes me to March.

 

Any ideas?

 

Thanks in advance.

 

<?PHP

if (isset ($_POST['back1']))
{
$date = time() - 269743;
$month = date('m',$date);
$day = date('d',$date);
$year = date('y',$date);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
}
elseif (isset ($_POST['forward1']))
{
$date = time() + 269743;
$month = date('m',$date);
$day = date('d',$date);
$year = date('y',$date);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
}
else{
$date = time();
$month = date('m',$date);
$day = date('d',$date);
$year = date('y',$date);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
}
?>

<form method="POST"  action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="application/x-www-form-urlencoded">

<?PHP

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;
}

$days_in_month = cal_days_in_month(0, $month, $year);

echo "<div align=center><table width=\"550\" height=\"440\" class=cal>";
echo "<tr><th colspan=7 bgcolor=#F3F3F3><input name=\"back1\" type=\"submit\" value=\"<<\" /> $title $year <input name=\"forward1\" type=\"submit\" value=\">>\" /></th></tr>";
echo "<tr align=\"center\"><td width=75 height=20>Sun</td><td width=75>Mon</td><td width=75>Tue</td><td width=75>Wed</td><td width=75>Thu</td><td width=75>Fri</td><td width=75>Sat</td></tr>";

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

$i = 0;
if($i%2 == 0) 
{ 
echo "<tr bgcolor='#F4F6FA'>"; 
$i++; 
}
else 
{ 
echo "<tr bgcolor='#FFF'>"; 
$i++; 
}
//first we take care of those blank days
while ( $blank > 0 ) 
{ 
echo "<td bgcolor=#FCFCFC></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 valign=\"top\" align\"left\" class=\"cal\"> $day_num </td>"; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr>";




if($i%2 == 0) 
{ 
echo "<tr bgcolor='#F4F6FA'>"; 
$i++; 
}
else 
{ 
echo "<tr bgcolor='#F3F3F3'>"; 
$i++; 
}





$day_count = 1;
}
}
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td bgcolor=#FCFCFC> </td>"; 
$day_count++; 
} 

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

?>

Link to comment
Share on other sites

Your problem is that your if segment that deals with going backwards (or forwards) only applies to the current date and time.

 

You would need to put in some type of conditional inside of those that would do your calculations based on the current month you're viewing, rather than always on the current actual month. This is assuming that $month will always be available as a variable in the script (since it's starting out in current month/year on a default page load).

 

if (isset ($_POST['back1']))
{
if ($month == date('m',time()))
{
$date = time() - 269743;
}
else
{
$date = strtotime("$month-1-$year");
}
$month = date('m',$date);
$day = date('d',$date);
$year = date('y',$date);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
}

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.