简体   繁体   中英

help, php calendar links

Below is a partial table row that will create a line row of 3 columns. Two columns in each side is links, left is previous year and right is next year links. Center is links for january thru december.

When you click a month links, the calendar will show that month you've clicked in the current year the calendar is on. For example, the calendar will open current month and year by default...March 2010. If you clicked previous year (2009), it will display current month (March) of last year (2009)...and then if you click Jun, the calendar will display June of whatever the year the calendar is currently on, which is June of 2009.

my question is, what can i do to following code to do such thing.

$Calendar.= "</tr><tr><td>"."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=" . $LastYear["year"] ."\"> $LastY </a></td>\n";
$Calendar.= "<td colspan=\"5\">"."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[0] ."\">Jan.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[1] ."\">Feb.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[2] ."\">Mar.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[3] ."\">Apr.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[4] ."\">May</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[5] ."\">Jun.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[6] ."\">Jul.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[7] ."\">Aug.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[8] ."\">Sep.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[9] ."\">Oct.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[10] ."\">Nov.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[11] ."\">Dec.</a>
              </td>";
$Calendar.= "<td>"."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=" . $NextYear["year"] ."\"> $NextY </a></td>\n";

Thanks in advance.

EDIT:

I should mention this first...only need to use PHP.

Thanks.

Okay. here is some hints to start from

As I said above, you have to make an HTML calendar for the current month. Start from just printing out day numbers. $num=date("t"); would give you number of days. So, I hope you can use for loop, from 1 to $num, printing each number in separate <td> tags.

Next, try to break that list in 7 days blocks. You can use a counter for this. Do increment it with each iteration, and as soon as it reach 7, add </tr><tr> to break a line.

Finally. you'd have to align this list to the week start. date("w") would help you with it.

Can you do that?

Maybe it would have been more clear if I had my whole code instead of just that partial table row that I needed help on. I've created the calendar already. I just wanted to add additional row to that calendar that I can click to open up new calendar by year and month.

If you can open this php on a browser, you'll notice the links below the calendar in three columns.
Left column is the link to open previous year from whatever the year is currently displayed.
Right column is the link to open next year from whatever the year is currently displayed. Center column is the links to each month from January thru December, whichever month is clicked, it will open that month calendar from whichever the year is "currently" displayed.

I hope this is more clear. I appologize for any confusion.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>test</title>
<link rel="stylesheet" type="text/css" href="calendar.css" />
</head>
<body><center>
<?php
$date = time(); 

$Month = date('m', $date); 
$Day = date('d', $date); 
$Year = date('Y', $date);

/*  Calendar layout*/
function calendar_layout($month,$day,$year)
{

/*  This checkdate function displays "bool(true)" in the web page, so I commented out  */
//  var_dump(checkdate($month,$day,$year));

$MonthArray = array("January ","February ","March ","April ","May ","June ","July ","August ","September ","October ","Novembe r","December ");
$ThisMonth = $MonthArray[$month-1];

/*  Creates table for calendar  */
$Calendar = '<table border="1" cellpadding="5">';
$Header = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

/*  Creates the first row print the Month and Year of this calendar  */
$Calendar.= '<tr><td colspan="7">';
$Calendar.= '<div class="largewords">'.$ThisMonth.''.$year.'</div>';
$Calendar.= '</td></tr>';

/*  Creates the second row for Week days and print the week days header  */
$Calendar.= '<tr>
               <td class="weekdays">'.implode('</td><td class="weekdays">',$Header).'</td>
             </tr>';

/*  Finds out and set how many blank days before 1st day  */
$FirstDay = mktime(0,0,0,$month, 1, $year);  
$DayOfWeek = date('D', $FirstDay);
switch($DayOfWeek){
case "Sun": $BlankDays = 0; break;
case "Mon": $BlankDays = 1; break;
case "Tue": $BlankDays = 2; break;
case "Wed": $BlankDays = 3; break;
case "Thu": $BlankDays = 4; break;
case "Fri": $BlankDays = 5; break;
case "Sat": $BlankDays = 6; break;
}   

/*  Finds out how many total days this month  */
$DayInMonth = date('t',mktime(0,0,0,$month,1,$year));
$DayCounter = 0;

/*  Creates the second row for first week  */
$Calendar.= '<tr>';

/*  Prints the blank spaces before day 1  */
while($DayCounter < $BlankDays)
{
  $Calendar.= '<td>&nbsp;</td>';
  $DayCounter++;
}

/*  Starts printing dates  */
for($PrintDay = 1; $PrintDay <= $DayInMonth; $PrintDay++):
  $Calendar.= '<td class="eachday">';
  $Calendar.= '<div class="numericdays">'.$PrintDay.'</div>';
  $Calendar.= str_repeat('<p>&nbsp;</p>',2);

  $Calendar.= '</td>';
  if($BlankDays == 6):
    $Calendar.= '</tr>';
    if(($DayCounter+1) != $DayInMonth):
      $Calendar.= '<tr>';
    endif;
    $BlankDays = -1;
    $DayInWeek = 0;
  endif;
  $DayInWeek++; $BlankDays++; $DayCounter++;
endfor;

/*  Prints blank spaces after the last day of the month  */
if($DayInWeek < 8):
  for($x = 1; $x <= (8 - $DayInWeek); $x++):
    $Calendar.= '<td>&nbsp;</td>';
  endfor;
endif;


/*  Bottom row - links for other years and months  */

$LastYear = getDate(mktime(0, 0, 0, $month, 1, $year-1));
$ThisYear = getDate(mktime(0, 0, 0, $month, 1, $year));
$NextYear = getDate(mktime(0, 0, 0, $month, 1, $year+1));

$LastY = $LastYear["year"];
$ThisY = $ThisYear["year"];
$NextY = $NextYear["year"];

/*  Initializing the years range from 1970 to 2038  */
$MinYear = date("Y",mktime(0,0,0,1,1,1970));
$MaxYear = date("Y",mktime(0,0,0,1,1,2038));

$Calendar.= "</tr><tr><td>"."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=" . $LastYear["year"] ."\"> $LastY </a></td>\n";
$Calendar.= "<td colspan=\"5\">"."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[0] ."\">Jan.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[1] ."\">Feb.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[2] ."\">Mar.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[3] ."\">Apr.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[4] ."\">May</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[5] ."\">Jun.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[6] ."\">Jul.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[7] ."\">Aug.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[8] ."\">Sep.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[9] ."\">Oct.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[10] ."\">Nov.</a> | 
                               "."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=".$ThisYear["year"]."&month=" . $MonthArray[11] ."\">Dec.</a>
              </td>";
$Calendar.= "<td>"."<a "."href=\"".$_SERVER["PHP_SELF"]."?year=" . $NextYear["year"] ."\"> $NextY </a></td>\n";
}


/*  Close the table  */
$Calendar.= '</tr></table>';

return $Calendar;

}

echo '<a href="index.php">Today</a>', ' is: ', date('l, F jS Y');
echo "<br />";
echo "<br />";

echo calendar_layout("$Month","$Day","$Year");

?> 

</center>
</body>
</html>

and while I was looking through that site that Leo posted, I've found out something. If I understood correctly, the logic is to code to check whether $Month & $Year variables are set or not for this particular section.

So I found this if statment:

if(isset($_GET["Year"]))
{
  use local system's year
}
else
{
  $Year = $_GET["Year"]
}

Now the problem is...how to figure out the syntax and apply it to my original code?

首先,您必须编写一个日历例程,该例程接受$ year和$ month参数并绘制一个月日历。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM