doudu3961 2010-12-31 00:29
浏览 74
已采纳

在循环递增变量内添加上一个日期的天数

I am working on a function which adds a number of days to a date inside an incrementing variable loop. I am having a problem getting the date from the previous loop to add the next 30 days to that date. This seems to be working for the first 2 loops then breaks and I cannot figure out the correct code to get the previous dates.

Here is my code:

$pay_cycles=5;
$period=30;

 $arr = array();
 for ($i=1;$i<=$pay_cycles;$i++) {

 //if first loop get todays date
 if($i==1){
 $due = date("Y-m-d");

 //else add to previous date
 } else {
 $time = strtotime ( '+'.$period.' day' , strtotime ( $due-1 ) ) ;
 $due = date("Y-m-d", $time); 
 }
   $arr[] = $due;
 }
 print_r($arr);

This is what prints

Array ( [0] => 2010-12-30 [1] => 2011-01-29 [2] => 2011-01-29 [3] => 2011-01-29 [4] => 2011-01-29 )

Thanks for looking

  • 写回答

3条回答 默认 最新

  • douban2014 2010-12-31 00:53
    关注

    Maybe I am not understanding your requirement completely: but to get the next 5 X 30 day periods:

    $pay_cycles=5;
    $period=30;
    
     $arr = array();
     for ($i=1;$i<=$pay_cycles;$i++) {
    
     //if first loop get todays date
     if($i==1){
     $due = date("Y-m-d");
    
     //else add to previous date
     } else {
     $time = strtotime ( "$due +$period day" ) ;
     $due = date("Y-m-d", $time); 
     }
       $arr[] = $due;
     }
     print_r($arr);
    

    Gives:

    Array
    (
        [0] => 2010-12-31
        [1] => 2011-01-30
        [2] => 2011-03-01
        [3] => 2011-03-31
        [4] => 2011-04-30
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题