首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >while PHP上的无限循环

while PHP上的无限循环
EN

Stack Overflow用户
提问于 2018-07-09 05:30:13
回答 1查看 256关注 0票数 0

首先,我不是一个程序员(只是一些PHP知识)。我做了一个网站的事件列表与选择字段过滤每月的事件与Ajax加载。

这段代码完美地运行了大约3个月。但在5天前,它停止了工作,开始生成无限循环。

我调试了代码,发现"while“循环就是问题所在。我在试着理解为什么这个方法在一段时间内工作得很好,然后它就不会了。(用无限循环使服务器崩溃)。

这个函数只给出下个月的第一天。它运行良好。

代码语言:javascript
复制
function first_day_next_month($month, $year){

  $month = str_replace("0", "", $month);

  if ($month == 12){
    return ($year+1)."0101";
  }else{
    return $year.str_pad($month+1, 2, "0", STR_PAD_LEFT )."01";
  }

}

这个选项为选择字段生成“选项”:

代码语言:javascript
复制
function ddown(){

  $start = strtotime("10 December 2017");

  $end = strtotime("+3 months");

  $current = $start;

  $meses = array(

    "Jan" => "enero",
    "Feb" => "febrero",
    "Mar" => "marzo",
    "Apr" => "abril",
    "May" => "mayo",
    "Jun" => "junio",
    "Jul" => "julio",
    "Aug" => "agosto",
    "Sep" => "septiembre",
    "Oct" => "octubre",
    "Nov" => "noviembre",
    "Dec" => "diciembre"

    );
  $o = "";

  $selected = "";
  $cls = " class=\"gray-out\"";
  while ($current < $end){

    if(date("m", $current) == date("m", strtotime("today")) AND date("Y", $current) == date("Y", strtotime("today"))){
      $selected = " selected=\"selected\"";
      $cls = "";
    }else{
      $selected = "";
    }



    $current = strtotime(first_day_next_month( date("m", $current), date("Y", $current) ));
  }

  return $o;

}

我发现这段代码现在正在生成无限循环:

代码语言:javascript
复制
while ($current < $end){

    if(date("m", $current) == date("m", strtotime("today")) AND date("Y", $current) == date("Y", strtotime("today"))){
      $selected = " selected=\"selected\"";
      $cls = "";
    }else{
      $selected = "";
    }



    $current = strtotime(first_day_next_month( date("m", $current), date("Y", $current) ));
  }

$current变量没有在while上更新。据我所知,这应该需要2017年12月10日到today+3月,以及这些日期之间每个月的输出和“选项”。

对如何解决这个问题有什么建议吗?

(对不起,我的英语)。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-09 05:55:36

在你的first_day_next_month函数中,你有:

代码语言:javascript
复制
$month = str_replace("0", "", $month);

这将用1替换10,这将在每年10月份恢复到1月份。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51236129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档