我在这里写了一个 PHP 日历生成器,http://shodor.org/~amalani/portfolio/apprenticeship/summer/phpstuff/calendar.php,它可以工作,除了 111 和 1753 之间的年份。我已经确定问题出在这一行
$first=date('w',mktime(0, 0, 0, $month, 1,$year));
它决定了该月第一天的数字表示。这将返回 -1 作为日期,因此日历函数永远不会开始新的一周。
这是所有代码
<?php
$date=getdate();
?>
<form action="calendar.php" method="POST">
<input type='number' name='year' value=
<?php if(isset($_POST['year'])){echo $_POST['year'];}else{echo $date['year'];}?>
/>
<select name="month">
<?php
$m=1;
for($x=60;$x<400;$x+=30){
$month=jdmonthname($x,1);
if(isset($_POST['month'])){
$selected=($m==$_POST['month'])?"selected='selected'":"";
echo $selected;
}
echo "
<option value='$m' $selected>$month</option>";
$m++;
}
?>
</select><br><br>
<input type='submit'/>
</form>
<table border='1'>
<tr>
<td>Sunday</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td>
</tr>
<?php
if(isset($_POST['month'])){
$year=$_POST['year'];
$month=$_POST['month'];
$days=cal_days_in_month(CAL_GREGORIAN,$month,$year);
//Get the numerical representation of the first day of the month
$first=date('w',mktime(0, 0, 0, $month, 1,$year));
//This tabs over until the appropriate day is reached in the beginning of the month
$week=1;
//Output the month and year
echo date('F',mktime(0,0,0,$month))." ". $year." Calendar";
//Start a new week
echo "<tr>";
for($x=1;$x<=$days;$x++){
$day=date('w',mktime(0,0,0,$month,$x,$year));
if($week==1){
for($y=0;$y<$first;$y++){
echo "<td></td>";
}
}
//Starts new week
if($day==0){
echo "</tr><tr><td>$x</td>";
}else{
echo "<td>$x</td>";
}
$week++;
}
echo "</tr>";
}
?>
</table>