我在这里编写了一个 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>
最佳答案
问题在于您使用 mktime()
请注意 php 手册中的粗体部分
year
The number of the year, may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On systems where time_t is a 32bit signed integer, as most common today, the valid range for year is somewhere between 1901 and 2038. However, before PHP 5.1.0 this range was limited from 1970 to 2038 on some systems (e.g. Windows).
您需要消除对 mktime()
的需要来解决此问题,可能通过使用 OOP Datetime class然而,对于年龄小于 100 的情况,这有其自身的局限性
这个answer可能会帮助您进一步了解问题
关于php - 为什么这个历法在 111 年和 1753 年之间中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17679649/