我正在尝试许多方法,但是后来我陷入了困境。

假设订单是今天创建的。我需要显示下一次重复订单的发生时间。因此,我已经创建了2012年6月13日的订单。然后,我将时间表设置为每两个月的第1个月一次,每两月一次的重复订单。如何计算下一次重复订单的发生时间?答案是8月1日。

如果有人可以概述一种方法,那将非常有用,它不一定是代码。这是我到目前为止所拥有的...

    // first, get starting date
    $start_date_month = date('m', strtotime($start_date));

    // get this year
    $this_year = date('Y');

    // if this month is december, next month is january
    $this_month = date('m', $timestamp_month);
    if($this_month == 12){
      $next_month = 1;
    // if this month is not december add 1 to get next month
    }else{
      $next_month = $this_month + 1;
    }

    // get array of months where recurring orders will happen
    $months = array();
    for ($i=1; $i<=6; $i++) {
      $add_month = $start_date_month+(2*$i); // 2, 4, 6, 8, 10, 12
      if($add_month == 13){$add_month = 1;$year = $this_year+1;}
      elseif($add_month == 14){$add_month = 2;$year = $this_year+1;}
      elseif($add_month == 15){$add_month = 3;$year = $this_year+1;}
      elseif($add_month == 16){$add_month = 4;$year = $this_year+1;}
      elseif($add_month == 17){$add_month = 5;$year = $this_year+1;}
      elseif($add_month == 18){$add_month = 6;$year = $this_year+1;}
      elseif($add_month == 19){$add_month = 7;$year = $this_year+1;}
      elseif($add_month == 20){$add_month = 8;$year = $this_year+1;}
      else{$year = $this_year;}
      echo $what_day.'-'.$add_month.'-'.$year.'<br />';
      $months[] = $add_month;
    }

    echo '<pre>';
    print_r($months);
    echo '</pre>';

我不想简单地查找从现在起两个月后的日期。假设创建的订单是6月1日。下一个重复订单是8月1日。现在,现在是9月1日,但是下一个重复订单是10月1日。看到我的困境了吗?

最佳答案

就拿当月来说,所以从六月开始,我们得到6. 6 mod 2 == 0。下个月是7月,我们得到7. 7 mod 2 == 1

因此,只需检查current month % 2 == (first month % 2)即可。

然后只需检查是否是每月的1号即可。

在PHP中,模数用百分比符号定义。

$month = date('n');
$createdMonth = 6;
if($month % 2 == $createdMonth % 2){
    // stuff
}

10-07 12:44