Jump to content

How to get calendar days as links


billy_111

Recommended Posts

Hey,

 

I have made a basic calendar on this page:

 

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

 

I have this code:

 

	 <?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=1 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> <a href='general-courses/?day=$day_num $title $year'>$day_num</a></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 "</table>";
	?> 

 

I have managed to get the days as links, however i need to also have an arrow to flip between months. How can i do this?

 

Hopefully someone can help.

 

Thanks again

 

Regards

Billy

Link to comment
https://forums.phpfreaks.com/topic/203774-how-to-get-calendar-days-as-links/
Share on other sites

Here's this rewrite of the top part of your code to get your started reading:

 

//This puts the current day, month, and year in seperate variables
$day = date('d') ;
$month = date('m') ;
$year = date('Y') ;
//Here we generate the first day of the month
$first_day = date('Y-m-01');
//This gets us the month name
$title = date('F') ; 
//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$blank = date('w', $first_day);
//We then determine how many days are in the current month
$days_in_month = date('t');
die(print_r(get_defined_vars(), true));

 

The last line will print out all defined variables to the screen so you can test that all the values are what they say they are.

Change:

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

To:

$date = (!isset($_GET['month']) && !isset($_GET['year'])) ? time() : strtotime($_GET['month'] . '/1/' . $_GET['year']);

 

Your links should be:

//find this line:
echo "<tr><th colspan=7> $title $year </th></tr>";
//add links here (suggestion only);
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>";

Hope there is no errors in there.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.