PHP Calendar Next/Prev link to months

Hey,

I have made a PHP calendar and have managed to have it linking to a page, if you look on this page:

http://www.glofamily.com/glo/

At the bottom, you can see the calendar, now what i need to do is have Next/Prev links that flip through months. Is this possible?

I currently have this code:


		 <div id="calendar" class="right">
		<?php
			$date = time () ;
			
			$day = date('d', $date) ;
			$month = date('m', $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) ;
			
			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) ;
		?>
		
		 <div id="php-calendar" class="right">
		 <table>
		 <tr><th colspan='7'><? echo $title. " ".$year; ?></th></tr>
		 <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>
			
		<?
		 $day_count = 1;
		
		 echo "<tr>";
		
		 while ( $blank > 0 )
		 {
			 echo "<td></td>";
			 $blank = $blank-1;
			 $day_count++;
		 }
		
		 $day_num = 1;
		
		 while ( $day_num <= $days_in_month )
		 {
			 echo "<td> <a href='http://www.glofamily.com/glo/general-courses/?day=$day_num $title $year'>$day_num</a></td>";
			 $day_num++;
			 $day_count++;
		
			 if ($day_count > 7)
			 {
			 echo "</tr><tr>";
			 $day_count = 1;
			 }
		 }
		
		 while ( $day_count >1 && $day_count <=7 )
		 {
		 echo "<td> </td>";
		 $day_count++;
		 }
		 ?>

		</table>		
		</div>

I would really appreciate it if someone can help me out?

Thanks again.

Yes it is. You will want to have the prev/next buttons submit to the same page, where hidden values pass the values for the previous and next months and years, and submit button buttons that pass whether you want the previous or the next one.


// presumes the existence of $calendarMonth and $calendarYear
$date = strtotime($calendarYear . '/' . $calendarMonth . '/01);
$prevDate = strtotime('last month', $date);
$nextDate = strtotime('next month', $date);
$prevMonth = date('m', $prevDate);
$prevYear = date('Y', $prevDate);
$nextMonth = date('m', $nextDate);
$nextYear = date('Y', $nextDate);
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
    <p>
        <input type="hidden" name="prevmonth" value="' . $prevMonth . '">
        <input type="hidden" name="prevyear" value="' . $prevYear . '">
        <input type="hidden" name="nextmonth" value="' . $nextMonth . '">
        <input type="hidden" name="nextyear" value="' . $nextYear . '">
        <input type="submit" name="prev" value="Prev">
        <input type="submit" name="next" value="Next">
    </p>
</form>';

And when the page loads, you can check if prev or next has been submitted.


$calendarMonth = date('m');
$calendarYear = date('Y');
if (filter_input(INPUT_POST, 'prev')) {
    $calendarMonth = filter_input(INPUT_POST, 'prevmonth', FILTER_VALIDATE_INT);
    $calendarMonth = filter_input(INPUT_POST, 'prevyear', FILTER_VALIDATE_INT);
}
if (filter_input(INPUT_POST, 'next') {
    $calendarMonth = filter_input(INPUT_POST, 'nextmonth', FILTER_VALIDATE_INT);
    $calendarMonth = filter_input(INPUT_POST, 'nextyear', FILTER_VALIDATE_INT);
}

However, if you instead want a calendar that can be updated without reloading the page, jQuery has a nice datepicker that can be scripted in many different ways.

Hey,

Thanks for your reply, i will try this now.

Just one thing, would i be able to use the jQuery calendar in the same way i am using it at the moment?

I would prefer it if the page didn’t reload, and also would want each day to link to a page. Can i do this?

Thanks

If you don’t want the page to reload then you won’t be using a PHP solution.

You should still have a PHP solution for those who do not run javascript. Then for those who do run javascript, you can instead use scripting to disable the calendar and replace it with the javascript one.

I suggest that once you get the PHP version working as you require, that you then head on over to the javascript forum and ask them about using ajax to update the datepicker calendar. The basic idea will be to use ajax to request from the server for the previous and next month and year, so that you can then quickly update the links when you show the new calendar page.

Hey,

I managed to get the calendar linking to other months. I updated the code as shown:


		 <div id="calendar" class="right">
		<?php
			$date = (!isset($_GET['month']) && !isset($_GET['year'])) ? time() : strtotime($_GET['month'] . '/1/' . $_GET['year']);
			
			$day = date('d', $date) ;
			$month = date('m', $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) ;
			
			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) ;
		?>
		
		 <div id="php-calendar" class="right">
		 <table>
		 <!--<tr><th colspan='7'><? echo $title. " ".$year; ?></th></tr>-->
		 <?php echo "<tr><th colspan=7><a href=\\"?month=". ($month - 1) . "&year=$year\\"><<</a> $title $year <a href=\\"?month=" . ($month + 1) . "&year=$year\\">>></a></th></tr>"; ?>
		 <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>
			
		<?
		 $day_count = 1;
		
		 echo "<tr>";
		
		 while ( $blank > 0 )
		 {
			 echo "<td></td>";
			 $blank = $blank-1;
			 $day_count++;
		 }
		
		 $day_num = 1;
		
		 while ( $day_num <= $days_in_month )
		 {
			 echo "<td> <a href='general-courses/?day=$day_num $title $year'>$day_num</a></td>";
			 $day_num++;
			 $day_count++;
		
			 if ($day_count > 7)
			 {
			 echo "</tr><tr>";
			 $day_count = 1;
			 }
		 }
		
		 while ( $day_count >1 && $day_count <=7 )
		 {
		 echo "<td> </td>";
		 $day_count++;
		 }
		 ?>

		</table>		
		</div> </div>

It now flips through months perfectly, however i have one minor issue. When i click on a month for example the First of June, it links to a page like this:

http://www.glofamily.com/glo/general-courses/[B]?day=1 June 2010[/B]

How can i change it so that the date is like this: ?day=01 June 2010

Can i do this?

Thanks

You could use something like:


$leadingZero = ($day_num < 10) ? '0' : '';

Or:


$day_num = str_pad($day_num, 2, "0", STR_PAD_LEFT);

Or:


$day_num = date('d', strtotime("$year-$month-$day"));

Hey,

Thanks guys, i think i have not explained properly. I dont want to show the actual days as 01, 02, 03 i want to when the link is clicked take users to the required page like so [

[/QUOTE]](http://www.glofamily.com/glo/general-courses/?day=01%20June%202010)

So the calendar would look exactly the same as it does now, for example on the calendar if i click on ‘1’ in the Month of June it would have a value in the querystring as 01. So i need to convert the date somehow. I tried using number_format but that didn’t work.

Anyway i added this line:

[QUOTE]
$day_num = 1; //Just after this line
$day_num = str_pad($day_num, 2, “0”, STR_PAD_LEFT);

And it converts ‘1’ to ‘01’. Can i achieve what i have explained above?

Thanks again

This post has been chosen to be answered by the Internet Oracle.

> i want to when the link is clicked take users to the required page

} Hello dear supplicant,
}
} It appears that you are using the $day_num variable in two
} different places even though you require different content.
}
} I suggest that you use two different variables so that you can
} apply different content in different places.
}
} You owe the Oracle a way to relive his glory days
}
} Signed,
} The Internet Oracle

hey i begginer in php and i would like some help.
in this calendar there is a problem…
when someone trying to change year it goes to 1970.
can some one help with that.

What is the code that you’re having trouble with?

i find the code from above.
thanks for replying

<div id=“calendar” class=“right”>

    &lt;?php 
		//This gets today's date 
        $date = (!isset($_GET['month']) && !isset($_GET['year'])) ? time() : strtotime($_GET['month'] . '/1/' . $_GET['year']);

        
		//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
        $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) ; 

    ?&gt;

     

     &lt;div id="php-calendar" class="right"&gt;

     &lt;table&gt;

     &lt;!--&lt;tr&gt;&lt;th colspan='7'&gt;&lt;? echo $title. " ".$year; ?&gt;&lt;/th&gt;&lt;/tr&gt;--&gt;
	 
		
		
     &lt;?php echo "&lt;tr&gt;&lt;th colspan=7&gt;&lt;a href=\\"?month=". ($month - 1) . "&year=$year\\"&gt;&lt;&lt;&lt;/a&gt; $title $year &lt;a href=\\"?month=" . ($month + 1) . "&year=$year\\"&gt;&gt;&gt;&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt;"; ?&gt;
	
     &lt;tr&gt;&lt;td width=42&gt;S&lt;/td&gt;&lt;td width=42&gt;M&lt;/td&gt;&lt;td width=42&gt;T&lt;/td&gt;&lt;td width=42&gt;W&lt;/td&gt;&lt;td width=42&gt;T&lt;/td&gt;&lt;td width=42&gt;F&lt;/td&gt;&lt;td width=42&gt;S&lt;/td&gt;&lt;/tr&gt;

        

    &lt;?php

     $day_count = 1;

    

     echo "&lt;tr&gt;";

     

     while ( $blank &gt; 0 ) 

     { 

         echo "&lt;td&gt;&lt;/td&gt;"; 

         $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 &lt;= $days_in_month ) 

     { 

         echo "&lt;td&gt; &lt;a href='general-courses/?day=$day_num $title $year'&gt;$day_num&lt;/a&gt;&lt;/td&gt;"; 

         $day_num++; 

         $day_count++;

    
		//Make sure we start a new row every week
         if ($day_count &gt; 7)

         {

         echo "&lt;/tr&gt;&lt;tr&gt;";

         $day_count = 1;

         }

     } 

     
		//Finaly we finish out the table with some blank details if needed
     while ( $day_count &gt;1 && $day_count &lt;=7 ) 

     { 

     echo "&lt;td&gt; &lt;/td&gt;"; 

     $day_count++; 

     } 

     ?&gt; 



    &lt;/table&gt;        

    &lt;/div&gt; &lt;/div&gt;

This would be due to a month of 13 being invalid.

A simple solution is to perform some bounds checking on the month, so that the right arrows, that go to the next month, use the following for the month

($month % 12 + 1)

and the following for the year

(intval($year) + ($month == 12 ? 1 : 0))