简体   繁体   English

帮助,PHP日历链接

[英]help, php calendar links

Below is a partial table row that will create a line row of 3 columns. 下面是部分表格行,该行将创建3列的行。 Two columns in each side is links, left is previous year and right is next year links. 每边两列是链接,左边是上一年,右边是明年链接。 Center is links for january thru december. 中心是1月至12月的链接。

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. 例如,日历默认情况下将打开当前月份和年份... 2010年3月。如果单击上一年(2009),它将显示上一年(2009)的当前月份(3月)...然后单击6月,日历将显示日历当前所在年份的6月,即2009年6月。

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. 我应该首先提到这一点...只需要使用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. 正如我上面所说,您必须为当月制作一个HTML日历。 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. 因此,我希望您可以使用for循环,从1到$ num,在单独的<td>标签中打印每个数字。

Next, try to break that list in 7 days blocks. 接下来,尝试在7天之内打破该列表。 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. 每次迭代都增加它,一旦达到7,就添加</tr><tr>来换行。

Finally. 最后。 you'd have to align this list to the week start. 您必须将此列表与一周的开始保持一致。 date("w") would help you with it. date("w")会帮助您。

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. 如果您可以在浏览器中打开此php,则会在日历下方的三列中看到链接。
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. 当我浏览Leo发布的网站时,我发现了一些东西。 If I understood correctly, the logic is to code to check whether $Month & $Year variables are set or not for this particular section. 如果我理解正确,那么逻辑就是编写代码来检查是否为此特定部分设置了$ Month和$ Year变量。

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参数并绘制一个月日历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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