【问题标题】:PHP - How to calculate one month or one year laterPHP - 如何计算一个月或一年后
【发布时间】:2017-04-21 00:37:53
【问题描述】:

我想用 PHP 计算 Recurly 计划的下一个计费日期。 有 2 种计费周期:每年 |每月一次。 我尝试使用 DateTime 和 DateInterval 类,但没有得到预期的结果。

<?php
$referenceTime = new \DateTime('2016-01-31');
$oneMonthLater = $referenceTime->modify('+1 month');
var_dump($oneMonthLater);
// public 'date' => string '2016-03-02 00:00:00.000000'

在 1 月 31 日加上一个月,我得到的是 3 月 2 日,而不是我预期的 2 月 29 日(或 28 日)。

8 月 31 日也是如此:

<?php
$referenceTime = new \DateTime('2016-08-31');
$oneMonthLater = $referenceTime->modify('+1 month');
var_dump($oneMonthLater);
// public 'date' => string '2016-10-01 00:00:00.000000'

如果每年,我预计 2016 年 2 月 29 日 + 1 年 => 2017 年 2 月 28 日

谢谢。

【问题讨论】:

  • 向我们展示你到目前为止所做的事情......我们会更容易回答。
  • $next_bill_date = new DateTime(); if ($plan_interval_unit == 'year') { $interval_spec = 'P'.$plan['plan_interval_length'].'Y'; } else if ($plan_interval_unit == 'month') { $interval_spec = 'P'.$plan['plan_interval_length'].'M'; } $next_bill_date->add(new DateInterval($interval_spec));
  • 请在您的问题中添加它。在这里很难阅读/理解...... :(

标签: php date recurly


【解决方案1】:

可能是这样的:

if (date('d') !== date('d', strtotime('+1 month'))
    date ('Y-m-d H:i:s', strtotime('last day of next month'));

if (date('d') !== date('d', strtotime('+1 year'))
    date ('Y-m-d H:i:s', strtotime('last day of this month next year'));

【讨论】:

    【解决方案2】:

    您可以使用 PHP 的 DateTime 对象调用 modify 来计算相对于当前日期的下一个日期。以下代码显示了您将如何处理您的具体情况。

    $next_bill_date = new DateTime();
    switch($plan_interval_unit) {
        case "year":
            $next_bill_date->modify("last day of next month");
            break;
    
        case "month":
            $next_bill_date->modify("last day of this month next year");
            break;
    }
    

    【讨论】:

      【解决方案3】:

      试试这个,如果date &gt; 28 use last day of next month 否则使用+1 month

      $get_date = strtotime("31-01-2016");
      $dt = explode("-",$get_date);
      $dt = $dt[0];
      var_dump(($dt > 28) ? date("d-m-Y", strtotime("31-01-2016 last day of next month")) : date("d-m-Y", strtotime("31-01-2016 +1 month")));
      

      DEMO

      【讨论】:

        【解决方案4】:

        你可以使用PHP内置的strtotime()函数

        // One month from today
        $date = date('Y-m-d', strtotime('+1 month'));
        
        // One month from a specific date
        $date = date('Y-m-d', strtotime('+1 month', strtotime('2016-12-06')));
        

        【讨论】:

        • 这并不能解决指定的问题,除非我遗漏了什么?如果我使用日期 2016-01-31 结果实际上是 Wed, 02 Mar 2016... 但 OP 正在寻找它返回 “2 月 29 日(或 28 日)”。
        • 您需要过去一个月还是未来一个月?!
        • 我自己回答。感谢您的帮助!
        【解决方案5】:
        function get_next_billing_date($now, $type, $format = 'Y-m-d')
        {
            $date = new DateTime($now);
        
            $y = $date->format("Y");
            $m = $date->format("m");
            $d = $date->format("d");
        
            if ($type == 'year') {
                $y++;
                if ($m == 2 && $d == 29) {
                    $d = 28;
                }
            } else if ($type == 'month') {
                if ($m == 12) {
                    $y++;
                    $m = 1;
                } else {
                    $m++;
                }
        
                $first_date = sprintf("%04d-%02d-01", $y, $m);
                $last_day_of_next_month = date('t', strtotime($first_date));
        
                if ($d > $last_day_of_next_month) {
                    $d = $last_day_of_next_month;
                }
            } else {
                // not supported
                return false;
            }
        
            $date->setDate($y, $m, $d);
        
            return $date->format($format);
        }
        

        【讨论】:

          猜你喜欢
          • 2010-10-01
          • 1970-01-01
          • 2022-11-08
          • 2019-09-04
          • 1970-01-01
          • 2013-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多