Jump to content

[SOLVED] Using a calendar to go to next month. HOW?!


iPixel

Recommended Posts

So i've got this awesome easy and simple calendar script.

It shows the current month, it's days in a nice neat fashion.

Exactly what i want, now i just want to add onto it and allow the user to view previous and next months.

 

Here's my script - I need help figuring out how to tell it to show next and previous months.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
table {
    width:210px;
    border:0px solid #888;    
    border-collapse:collapse;
}

td {
    width:30px;
    border-collpase:collpase;
    border:1px solid #888;
    text-align:right;
    padding-right:5px;
}

.days{
    background-color: #F1F3F5;
}

th {
    border-collpase:collpase;
    border:1px solid #888;
    background-color: #E9ECEF;
}

.actday{
    background-color: #c22;
    font-weight:bold;
}
</style>
</head>
<body>

<?php
function showCalendar(){
    // Get key day informations. 
    // We need the first and last day of the month and the actual day
    $today    = getdate();
    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
    
    
    // Create a table with the necessary header informations
    echo '<table>';
    echo '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
    echo '<tr class="days">';
    echo '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
    echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
    
    
    // Display the first calendar row with correct positioning
    echo '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
        echo '<td> </td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
        $actday++;
        if ($actday == $today['mday']) {
            $class = ' class="actday"';
        } else {
            $class = '';
        }
        echo "<td$class>$actday</td>";
    }
    echo '</tr>';
    
    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);
    
    for ($i=0;$i<$fullWeeks;$i++){
        echo '<tr>';
        for ($j=0;$j<7;$j++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            echo "<td$class>$actday</td>";
        }
        echo '</tr>';
    }
    
    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
        echo '<tr>';
        
        for ($i=0; $i<7;$i++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            
            if ($actday <= $lastDay['mday']){
                echo "<td$class>$actday</td>";
            }
            else {
                echo '<td> </td>';
            }
        }
        
        
        echo '</tr>';
    }
    
    echo '</table>';
}

showCalendar();
?>

</body>
</html> 

 

Thanks 2 all who help!

Link to comment
Share on other sites

Yea, near the top header where it says Month - Year i just want to add << and >> to skip through the months, i've never worked with dates before so im not entirely sure where to tell it ok you're not looking at current month but at month #11 of 2009.

 

Here's where i got my script, but i dont see any help there either.

http://www.phptoys.com/e107_plugins/content/content.php?content.33.2

 

my best guess is the main change will occur here

    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));

 

Thanks

 

Link to comment
Share on other sites

Well it's only scrolling through by month, so I would use negative for months before today's month and positive number for months after today.

 

Something like this -

 

<<    May    >>

^      ^      ^

-1    0        1

 

And if you selected -1, then get today's date minus 1 month. Then the map should look like

 

<<  April    >>

^      ^      ^

-2    -1      0

Link to comment
Share on other sites

What you need to do is modify the following:

<?php
    // Get key day informations. 
    // We need the first and last day of the month and the actual day
    $today    = getdate();
    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
?>

 

That code loads the first and last day of the month according to todays date. Passing a global variable and drawing it using $_GET will allow you to manipulate that portion. We can quite simply just do this if we pass "month" and "year" though the address bar:

 

<?php
$next[0] = $today['mon'] + 1;
if ($next[0] > 12)
{
$next[0] = 1;
$next[1] = $today['year'] + 1;
}
$prev[0] = $today['mon'] - 1;
if ($prev[0] < 1)
{
$prev[0] = 12;
$prev[1] = $today['year'] - 1;
}

$link[0] = 'calendar.php?month=' . $next[0] . '&year=' . $next[1];
$link[1] = 'calendar.php?month=' . $prev[0] . '&year=' . $prev[1];
?>

Link to comment
Share on other sites

Yep i managed to figure this out so far, im having 1 silly issue... $today['month']... at the moment it's MAY but $today['month']+1 wont make it June lol, how can i make it +1, im trying to avoid using $today['mon'] and the do a switch statement 1 through 12 assigning names.

 

Thanks for the help

Link to comment
Share on other sites

LOL turns out that script is bugged, Feb of 09 and Nov of 09 bug out because the script starts the days off with monday thru sunday as opposed to sunday thru saturday. Ah well but at least i have a starting point.

 

Thanks guys!

Link to comment
Share on other sites

For those that would search this in the future, here is the full working script with the month skipping ability :)

 

ENJOY !!!

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Calendar</title>
<style type="text/css">
table
{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#000000;
border: 1px solid #CCCCCC;
width:200px;
}
th
{
color:#993333;
background-color:#e7e7e7;
border:1px solid #CCCCCC;
width:28px;
}
td
{
border:1px solid #CCCCCC;
text-align:right;
}
.main_th
{
color:#FFFFFF;
font-weight:bold;
background-color:#993333;
border:1px solid #CCCCCC;
text-align:center;
}
.actday
{
color:#993333;
font-weight:bold;
background-color:#FFFFCC;
}
.prevnext a:link
{
	color:#FFFFFF;
	text-align:right;
	font-size:12px;
	font-weight:bold;
	text-decoration:none;
}
.prevnext a:visited
{
	color:#FFFFFF;
	text-align:right;
	font-size:12px;
	font-weight:bold;
	text-decoration:none;
}
.prevnext a:hover
{
	color:#FFFF00;
	text-align:right;
	font-size:12px;
	font-weight:bold;
	text-decoration:none;
}

</style>
</head>
<body>

<?php
function showCalendar()
{
$today = getdate();

$set_month = $_GET['M'];
if($set_month == "")
	{ $set_month = $today['mon']; }
else
	{ $set_month = $_GET['M']; }

$set_year = $_GET['Y'];	
if($set_year == "")
	{ $set_year = $today['year']; }
else
	{ $set_year = $_GET['Y']; }

$firstDay = getdate(mktime(0,0,0,$set_month,1,$set_year));
    $lastDay  = getdate(mktime(0,0,0,$set_month+1,0,$set_year));

switch($set_month)
	{
		case 1:
			$mymonth = "January";
			break;
		case 2:
			$mymonth = "February";
			break;
		case 3:
			$mymonth = "March";
			break;
		case 4:
			$mymonth = "April";
			break;
		case 5:
			$mymonth = "May";
			break;
		case 6:
			$mymonth = "June";
			break;
		case 7:
			$mymonth = "July";
			break;
		case 8:
			$mymonth = "August";
			break;
		case 9:
			$mymonth = "September";
			break;
		case 10:
			$mymonth = "October";
			break;
		case 11:
			$mymonth = "November";
			break;
		case 12:
			$mymonth = "December";
			break;
	}

//Array Returned from getdate();
//(
//    [seconds] => 40
//    [minutes] => 58
//    [hours]   => 21
//    [mday]    => 17       --  Numeric representation of the day of the month 1 to 31
//    [wday]    => 2        --  Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
//    [mon]     => 6        --  Numeric representation of a month 1 through 12 
//    [year]    => 2003     --  A full numeric representation of a year, 4 digits Examples: 1999 or 2003 
//    [yday]    => 167		--  Numeric representation of the day of the year 0 through 365
//    [weekday] => Tuesday  --  A full textual representation of the day of the week Sunday through Saturday 
//    [month]   => June		--  A full textual representation of a month, such as January or March : January through December
//    [0]       => 1055901520
//)

$next[0] = $set_month + 1;
if ($next[0] > 12)
	{
		$next[0] = 1;
		$next[1] = $set_year + 1;
	}
else
	{
		$next[1] = $set_year;
	}
$prev[0] = $set_month - 1;
if ($prev[0] < 1)
	{
		$prev[0] = 12;
		$prev[1] = $set_year - 1;
	}
else
	{
		$prev[1] = $set_year;
	}

$link[0] = 'calendar.php?M=' . $next[0] . '&Y=' . $next[1];
$link[1] = 'calendar.php?M=' . $prev[0] . '&Y=' . $prev[1];

echo '<table cellpadding="3" cellspacing="0" border="0">';
echo '<tr>
	    <td colspan="7" class="main_th"> <span class="prevnext"><a href="' .$link[1]. '"> <<</a></span>       ' . $mymonth . ' ( ' . $set_year . ' )       <span class="prevnext"><a href="' .$link[0]. '">>></a></span> </td>
	  </tr>';
echo '<tr>
		<th align="center">Sun</th>
		<th align="center">Mon</th>
		<th align="center">Tue</th>
		<th align="center">Wed</th>
		<th align="center">Thu</th>
		<th align="center">Fri</th>
		<th align="center">Sat</th>
	  </tr>';

// Display the first calendar row with correct positioning	  
echo '<tr>';
    for($i=0;$i<$firstDay['wday'];$i++)
	{
		echo '<td> </td>';
	}
    $actday = 0;
    for($i=$firstDay['wday'];$i<=6;$i++)
	{
		$actday++;
		echo "<td onmouseover=\"this.bgColor='#E7E7E7'\" onmouseout=\"this.bgColor='#FFFFFF'\">$actday</td>";
	}
    echo '</tr>';	  

//Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);
    
    for ($i=0;$i<$fullWeeks;$i++)
{
        echo '<tr>';
        for ($j=0;$j<7;$j++)
		{
			$actday++;
			if ($actday == $today['mday']) 
			{
				$class = 'class="actday"';
			} 
			else 
			{
				$class = '';
			}
			echo "<td $class onmouseover=\"this.bgColor='#E7E7E7'\" onmouseout=\"this.bgColor='#FFFFFF'\">$actday</td>";
		}
        echo '</tr>';
    }

//Now display the rest of the month
    if ($actday < $lastDay['mday'])
{
        echo '<tr>';
        for ($i=0; $i<7;$i++)
		{
			$actday++;
			if ($actday == $today['mday']) 
				{
					$class = 'class="actday"';
				} 
			else 
				{
					$class = '';
				}

			if ($actday <= $lastDay['mday'])
				{
					echo "<td $class onmouseover=\"this.bgColor='#E7E7E7'\" onmouseout=\"this.bgColor='#FFFFFF'\">$actday</td>";
				}
			else 
				{
					echo '<td> </td>';
				}
		}
        echo '</tr>';
    }
echo '</table>';

}
showCalendar();
?>

</body>
</html> 

 

~iPixel

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.