【问题标题】:How to fix correct the date in this case?在这种情况下如何修复正确的日期?
【发布时间】:2021-08-31 00:53:36
【问题描述】:

请告诉我,我们有一个日期,例如 2020-06-15。我需要提前一年增加一个月,并将日期更改为该月的最后一天。也就是说,结果应该是:

  • 2020-06-15(第一次约会)
  • 2020-07-31(7月31天)
  • 2020-08-31(8月31天)
  • 2020-09-30(9月30天)
  • 2020-10-31(10月31天)

以此类推,直到今年 7 月 22 日。

我尝试这样:

            foreach ($getDates as $getDate) {
            # Suppose getDate contains - 2020-06-15
            $firstDate = new \DateTime($getDate);
            $datesArr[] = $firstDate->format('Y-m-d');
                for ($i = 1; $i <= 12; $i++) {
                    $date = new \DateTime($getDate);

                    $month = $date->format('F');
                    $year = $date->format('Y');

                    $date->modify("+$i month");
                    # until this moment everything is fine, one by one months is added one year in advance
                    $date->modify("last day of $month $year");
                    # everything breaks here, although the documentation states "last day of July 2008"
                    $datesArr[] = $date->format('Y-m-d');
                }
        }

但是在 modify("last day of $month $year");告诉我怎么解决?

【问题讨论】:

    标签: php date


    【解决方案1】:

    您需要获取$month$year 中的值之后 更改日期$date-&gt;modify("+$i month");,而不是之前:

    <?php
    $getDate = '2020-06-15';
    $firstDate = new \DateTime($getDate);
    $datesArr[] = $firstDate->format('Y-m-d');
    for ($i = 1; $i <= 12; $i++) {
        $date = new \DateTime($getDate);
    
        $date->modify("+$i month");
        
        // ----- Changes here -----
        $month = $date->format('F');
        $year = $date->format('Y');
        // ------------------------
        
        $date->modify("last day of $month $year");
        
        $datesArr[] = $date->format('Y-m-d');
    }
    var_dump($datesArr);
    

    这个输出:

    array(13) {
      [0]=>
      string(10) "2020-06-15"
      [1]=>
      string(10) "2020-07-31"
      [2]=>
      string(10) "2020-08-31"
      [3]=>
      string(10) "2020-09-30"
      [4]=>
      string(10) "2020-10-31"
      [5]=>
      string(10) "2020-11-30"
      [6]=>
      string(10) "2020-12-31"
      [7]=>
      string(10) "2021-01-31"
      [8]=>
      string(10) "2021-02-28"
      [9]=>
      string(10) "2021-03-31"
      [10]=>
      string(10) "2021-04-30"
      [11]=>
      string(10) "2021-05-31"
      [12]=>
      string(10) "2021-06-30"
    }
    

    【讨论】:

      【解决方案2】:

      当使用“下个月的最后一天”时,它会变短一些。

      $getDate = '2020-06-15';
      $date = new \DateTime($getDate);
      $datesArr[] = $date->format('Y-m-d');
      
      for ($i = 1; $i <= 12; $i++) {
        $datesArr[] = $date
         ->modify("last day of next month")
         ->format('Y-m-d')
        ;
      }
      
      var_dump($datesArr);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-06
        • 2012-06-06
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多