移动数组内部指针超过最大执行时间

时间:2012-10-01 16:38:00

标签: php arrays calendar

我正在尝试构建一个能够显示3个迷你日历月的日历系统。上个月,本月和下个月。

下面的代码应该只是将数组指针移动到当前月份。我认为它在星期五(9月28日)工作,但是今天早上(10月1日)它会在日志中导致以下错误: PHP Fatal error: Maximum execution time of 30 seconds exceeded

我怀疑它是一个新月与它有什么关系,但我正在抓住想法。我希望有人能看到我在这里做错了什么,因为这对我来说都是正确的。

$thisMonth = date('m', time());

$arrMonths = array('01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', 
                    '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', 
                    '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December');

while (key($arrMonths) !== $thisMonth) 
    next($arrMonths);

1 个答案:

答案 0 :(得分:2)

这是因为您正在使用严格比较运算符:

while (key($arrMonths) !== $thisMonth) 
    next($arrMonths);

!==正在尝试匹配键的类型和内容;在这种情况下,因为您已使用单引号声明它们,所以您的键是字符串。它没有进行类型比较(你将字符串与整数进行比较),因此它将进入无限循环。

要修复它,只需使用更宽松的比较运算符:

while (key($arrMonths) != $thisMonth) 
    next($arrMonths);

感谢@MiDo - 我实际上错了:

the return value of date('m', time()); is a string and the keys are integers when they are >= 10.