微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP.如何获得双月重复活动?

我正在尝试很多方法,但后来我被卡住了一半.

假设今天创建了订单.我需要显示一个定期订单何时发生.所以我在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. 6 mod 2 == 0.下个月是7月,我们得到7. 7 mod 2 == 1.

因此,只需检查当前月份%2 ==(第一个月%2).

然后检查它是否是本月的第1天.

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

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

原文地址:https://www.jb51.cc/php/133160.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐