首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP迭代日期修改=每月的最后一天

问PHP迭代日期修改=每月的最后一天
EN

Stack Overflow用户
提问于 2017-07-24 20:24:34
回答 1查看 420关注 0票数 0

我做了一个应用程序,做了很多事情,但这里最重要的是,它需要一个startingDate,根据频率和持续时间,它会按这个月数修改日期。

例如:

代码语言:javascript
复制
$startingDate = new DateTime('2000-01-31');
$duration = 190;
$frequency = 12; //(monthly)
//So, if frequency is monthly, this date will iterative change 190 times by one month

如果我的起始日期是1月31日,然后加上1个月,它会返回3月2日的日期。我正在尝试做一个脚本,将以这种方式添加几个月的日期:

代码语言:javascript
复制
starting date : 31/01/2000
+1 month = 29/02/2000
+2 months = 31/03/2000
+3 months = 30/04/2000 
....
+13 months = 28/02/2001

另一种情况:

代码语言:javascript
复制
Starting date : 28/02/2001
+1 month : 31/03/2001
+2 months : 30/04/2001
...

所以,这个想法是..。如果开始日期为当月的最后一天,则结束日期必须为当月的最后一天。

所以,下面是我的代码:

代码语言:javascript
复制
// $date came from object
for($i = 0; $i < $duration; $i++){
    //calculation stuff
    $this->array[$i] = array(
                              'Date' => $date->format('d/m/y'),
                              'Capital' => $capital,
                              'Rate' => $rate,
                              );
                  $capital = $remaining;
    //end of calculation stuff
                  $months = (12/$frequency). ' months';
                  $h = 12/$frequency;

      $currentDay = $date->format('d');
      if($currentDay == $date->format('t') ) {  
            if ( $h == 1) { 
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');  
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');  
              $date->modify('last day of next month');
                  } 
            if ( $h == 6) { 
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');  
              $date->modify('last day of next month');
                  }         
          if ( $h == 4) { 
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
              $date->modify('last day of next month');
                  } 


           if ( $h == 12) { 
              $date->modify('last day of next ');
                  } 

           } else {
            $date->modify($months);
           }
         }//for

使用这段代码,当我输入开始日期2000年1月31日(这个月的最后一天)时,它以正确的方式给出了下一个值:

代码语言:javascript
复制
29/02/00,
31/03/00,
... 
28/02/01 
...

当我输入起始日期29/02/2000,频率=2(每半年)时,结果是:

代码语言:javascript
复制
29/02/00,
31/08/00,
28/02/00,
31/08/01 // (right calculations)

这里有一个错误:当我输入开始日期28/02/2000 (不是这个月的最后一天)时,它给出了我:

代码语言:javascript
复制
28/02/00,
28/08/00,
28/02/01 //(last day in this month),
31/08/01,
28/02/02,
31/08/02,
... //Error here.

当我设置开始日期为30/04/2000,频率为每月(12)时,结果是:

代码语言:javascript
复制
30/04/2000,
30/05/2000,
30/06/2000,
30/07/2000,
30/08/2000,
...
30/01/2001,
02/03/2001, 
02/04/2001, 
02/05,2001 
...

如何修复此错误和/或升级代码?

EN

回答 1

Stack Overflow用户

发布于 2017-07-24 20:39:14

"t“返回给定日期当月的天数:

代码语言:javascript
复制
$a_date = "2017-07-23";
echo date("Y-m-t", strtotime($a_date));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45280589

复制
相关文章

相似问题

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