PHP-如何计算一个月或一年后

PHP-如何计算一个月或一年后,php,date,recurly,Php,Date,Recurly,我想用PHP计算Recurly计划的下一个计费日期。 有两种类型的计费周期:每年|每月。 我尝试使用DateTime和DateInterval类,但没有得到预期的结果 <?php $referenceTime = new \DateTime('2016-01-31'); $oneMonthLater = $referenceTime->modify('+1 month'); var_dump($oneMonthLater); // public 'date' => string

我想用PHP计算Recurly计划的下一个计费日期。 有两种类型的计费周期:每年|每月。 我尝试使用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'

可能是这样的:

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'));

您可以使用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;
}

如果
日期>28
使用下个月的最后一天
或者使用
+1个月

$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")));

您可以使用PHP内置函数


让我们看看你到目前为止做了什么。。。我们将更容易回答。$next_bill_date=new DateTime();如果($plan_interval_unit='year'){$interval_spec='P'.$plan['plan_interval_length'.'Y'.}如果($plan_interval_unit='month'){$interval_spec='P'.$plan['plan_interval_length'.'M'}$next_bill_date->add(new DateInterval($interval_spec));请在你的问题中加上它。此处难以阅读/理解…:(这并不能解决指定的问题,除非我遗漏了什么?如果我使用日期
2016-01-31
,结果实际上是
Wed,2016年3月2日…
,但OP希望它返回“2月29日(或28日)”。你需要上个月还是下个月?!我自己回答。谢谢你的帮助!
$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")));
// 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')));
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);
}