所以我正在制作一份日历表格供客户选择要预订的课程......然后将其上传到课程与客户之间的链接表中,但我不能在生活中将日历放入日历中网格正常...我的代码简单而且甜美,但我需要做这个非面向对象(我有小经验,但没有其他)....
我一整天都在盯着这个,但无济于事!
(我正在学习,所以我不会走下保护它的路线......这是为了自学,所以只需要工作并帮助我理解我哪里出错了)
代码:
<?
if(@$_POST['month'])
{
$_SESSION['month'] = trim($_POST['month']);
}
elseif(!isset($_SESSION['month']))
{
$_SESSION['month'] = '3';
}
if(@$_POST['year'])
{
$_SESSION['year'] = trim($_POST['year']);
}
elseif(!isset($_SESSION['year']))
{
$_SESSION['year'] = '2012';
}
?>
<form name="sort" action="calender.php" method="post">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
<input type="submit" value=" - !Go! - " />
</form><br />
<?php
$month = $_SESSION['month'];
$year = $_SESSION['year'];
$day = 1;
$nummonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo "<form name='choosedate' action='datechosen' method='get'>";
for($day = 1; $day <= $nummonth; $day++)
{
echo $day."/".$month."/".$year."<input type='radio' name='coursedate value='".$day."-".$month."-".$year."' /><br />";
}
echo "<input type='submit' value=' - Submit - ' /></form>";
?>
感谢您提供任何帮助......任何建议,甚至只是正确方向的一点,都非常感谢!
答案 0 :(得分:3)
这是一个我应该使用当前代码的函数。要使用该功能,只需通过以下方式调用它:
# $month, $year must be numeric. ie. Aug = 8
# The 3rd parameter is optional. It will display the calendar month starting
# on either Monday or Sunday, depending on your preference. Default is 'sun',
# but anything other than 'sun' will cause the displayed week to start on Monday.
printCalendarMonth($month, $year, 'mon');
然后您需要包含以下代码。我在评论中提供了帮助。
function printCalendarMonth($month, $year, $firstDay = 'sun')
{
# Get the number of days in the month
$totalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
# Get the day-of-the-week the 1st starts on
$date = getDayOfTheWeek($month,$year, $firstDay);
# print out the table headers
printTableHeader($date, $firstDay);
# print table body
printTableBody($totalDays, $date['startDay']);
# print out the table headers
printTableFooter();
}
function printTableHeader($date, $firstDay)
{
# The standard "Sunday First" calendar of days
$days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
# change to the "Monday First" calendar
if($firstDay == 'mon'){
# the days of the week
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
}
# print the header HTML for the calendar table
echo '<h1>' . $date['month'] . ', ' . $date['year'] . '</h1>' . "\n";
echo '<table>' . "\n";
echo "\t" . '<thead>' . "\n";
echo "\t\t" . '<tr>' . "\n";
# loop through the days and print them out
foreach($days as $day){
echo "\t\t\t" . '<th>' . $day . '</th>' . "\n";
}
# close table header HTML
echo "\t\t" . '</tr>' . "\n";
echo "\t" . '</thead>' . "\n";
echo "\t" . '<tbody>' . "\n";
}
function printTableBody($totalDays, $startDay)
{
# value to represent the day the last day of the month falls on
$endDay = 6;
# value to represent the current day of the week
$currDay = 1;
# Loop through all the days of the month
for($todayDate=1; $todayDate <= $totalDays; $todayDate++){
# start a new row every 7 days
if($currDay % 7 == 1) { echo "\t\t" . '<tr>' . "\n"; }
# increment the current day (do this before the closing table row)
$currDay++;
# loop through the number of starting days to represent the days of the previous month
$currDay = printPrevMonthDays($startDay, $currDay);
# print out the table cell for this day. Start a new row for every 7 days
echo "\t\t\t" . '<td>'.$todayDate.'</td>' . "\n";
# close the table row every 7 days
if($currDay % 7 == 1) { echo "\t\t" . '</tr>' . "\n"; }
}
}
function printTableFooter($firstDay)
{
echo "\t" . '</tbody>' . "\n";
echo '</table>' . "\n";
}
function getDayOfTheWeek($month, $year, $firstDay = 'sun')
{
$date = array();
# save out the date values
$time = mktime(0, 0, 0, $month, 1, $year);
$date['dotw'] = strtolower( date("D", $time) );
$date['month'] = strtolower( date("F", $time) );
$date['year'] = strtolower( date("Y", $time) );
switch($date['dotw']){
default:
case 'sun': $date['startDay'] = 0; break;
case 'mon': $date['startDay'] = 1; break;
case 'tue': $date['startDay'] = 2; break;
case 'wed': $date['startDay'] = 3; break;
case 'thu': $date['startDay'] = 4; break;
case 'fri': $date['startDay'] = 5; break;
case 'sat': $date['startDay'] = 6; break;
}
# subtract one if we want to display Monday as the first day of the week
if($firstDay == 'mon')
$date['startDay'] = $date['startDay'] - 1;
# return date array object
return $date;
}
# passing startDay by reference
function printPrevMonthDays(&$startDay, $currDay)
{
while($startDay != 0){
echo "\t\t\t" . '<td>[blank]</td>' . "\n";
# increment the current day
$currDay++;
# decrement the startDay
$startDay--;
}
# return zero to
return $currDay;
}
主要概念是DaveyBoy说的,它只是一个X-by-X阵列。唯一真正的诀窍是,天不总是在一周的第一天开始,所以你需要打印出上个月的一些“空白”日。
你可以通过创建一个7×by-2D的2D数组然后循环执行此操作来轻松完成此操作,但它将是您需要填充空格的相同概念。
当然,那里有一些硬编码值,可以对其进行改进,但是为了本教程的目的,这里是。
希望它有所帮助! 干杯!
答案 1 :(得分:1)
我自己做了这件事但是花了很多时间。如今,我使用jQuery-ui。
内置了日期选择器,它非常易于配置。它可以做单独的日期或范围。我之前使用它并且很容易上手(即使我有限的javaScript经验)
http://jqueryui.com/demos/datepicker/
我认为这是我在下面的一条评论中描述的代码。对不起,如果它是错综复杂的 - 这是我早期作为PHP编码器。
<?php
/**
* Calendar functions
*/
function ConvDate(&$D)
{
$TempDate=substr($D,8,2)."/".substr($D,5,2)."/".substr($D,0,4);
$D=$TempDate;
}
/**
* Function to display a calendar of a month
*/
function DispCalendar($Month=NULL,$Year=NULL, $Link=NULL, $VarName=NULL,$highlight=NULL)
{
$DIM=array('01'=>31,'02'=>28,'03'=>31,'04'=>30,'05'=>31,'06'=>30,'07'=>31,'08'=>31,'09'=>30,'10'=>31,'11'=>30,'12'=>31);
/**
* Set default values for Month and Year if none supplied
*/
if (empty($Month))
{
$Month=date("m");
}
if (empty($Year))
{
$Year=date("Y");
}
/**
* Add a leading zero to the month number if required
*/
$Month=str_pad($Month,2,"0",STR_PAD_LEFT);
/**
* Check for a leap year. The rule is that the year is divisible by 4 or 400 (eg 2000). Year values with an odd number of centuries (eg 1900)
* are NOT leap years. Easiest way to deal with this is all years where there is a remainder when dividing by 100 and none when
* dividing by 4 or when there is no remainder when dividing by 400
* Use the "Exact equivalence" operator (===) as 0 is used as a value for "false"
*/
if ((((($Year % 4)===0) && (($Year % 100)!==0)) || ($Year % 400)===0) && ($Month==2))
{
$DaysInMonth=29;
} else
{
$DaysInMonth=$DIM[$Month];
}
/**
* If a link has been passed and no variable name or vice versa, clear them as it's pointless having one without the other
*/
if ((isset($Link) && !isset($VarName)) || (!isset($Link) && isset($VarName)))
{
unset ($Link);
unset ($VarName);
}
/**
* First day of the selected month
*/
$FOMTS=mktime(0,0,0,$Month,1,$Year);
/**
* Today's date
*/
$todayDay=date("d");
$todayMonth=date("m");
$todayYear=date("Y");
/**
* Which day of the week does the first fall on. Sunday=0
*/
$FirstFallsOn=date("w",$FOMTS);
/**
* Full month name
*/
$LongMonth=date("F",$FOMTS);
$LongYear=$Year;
/**
* Start a table
*/
echo "<table width=\"100%\" class=\"cal\">\n";
/**
* Display the month and the year
*/
echo "\t<tr>\n";
echo "\t\t<th colspan=7>$LongMonth - $LongYear</th>\n";
echo "\t</tr>\n";
/**
* Display the days of the week
*/ echo "\t<tr>\n";
foreach (array("Sun","Mon","Tue","Wed","Thu","Fri","Sat") as $day)
{
echo "\t\t<td width=\"14.28%\">$day</td>\n";
}
echo "\t</tr>\n";
/**
* Pad out the table cells until the correct day of the week has been reached for the first of the month. Keep
* track of the number of cells
*/
echo "\t<tr>\n";
for ($dayCounter=0; $dayCounter<$FirstFallsOn; $dayCounter++)
{
echo "\t\t<td></td>\n";
}
/**
* Start a counter to keep track of the number of days displayed
*/
$dayOfMonth=1;
/**
* Display the days of the month
*/
for ($dayOfMonth=1; $dayOfMonth<=$DaysInMonth; $dayOfMonth++)
{
/**
* If the date being displayed is today's date, highlight it in a colour
*/
$class=((($dayOfMonth==$todayDay) && ($Year==$todayYear) && ($Month==$todayMonth)) ? " class=\"today\"" : ($highlight==($dayOfMonth.$Month.$Year) ? " class=\"calChosen\"" : NULL));
echo "\t\t<td$class>";
if (isset($Link))
{
$FL="$Link?$VarName=";
$FL.=str_pad($dayOfMonth,2,"0",STR_PAD_LEFT).$Month.$Year;
echo "<a href=\"$FL\">$dayOfMonth</a>";
} else
{
echo $dayOfMonth;
}
echo "</td>\n";
$dayCounter++;
if (($dayCounter % 7)===0)
{
echo "\t</tr>\n\t<tr>\n";
}
}
for (;($dayCounter%7)!==0; $dayCounter++)
{
echo "\t\t<td></td>\n";
}
echo "\t</tr>\n";
echo "</table>\n";
}
?>