phplaraveldatetimephp-carbon

Laravel Carbon (How to calculate exit next payment date)


I need to get exit next date of payment of loan:

as loan date: 1-1-2020

loan period: 2 years 5 months

payment date: ? ?

calculating expected date of payment

// calculating next payment date
 date("Y-M-D", strtotime(date("2020-1-1")." + 2 years  + 2 months"))

Solution

  • Try this,

    // Loan start date
    $loanStartDate = new DateTime("2020-01-01");
    
    // Loan period: 2 years and 5 months
    $loanPeriod = new DateInterval("P2Y5M");
    
    // Add the loan period to the loan start date
    $nextPaymentDate = clone $loanStartDate;
    $nextPaymentDate->add($loanPeriod);
    
    // Output the next payment date
    echo $nextPaymentDate->format("Y-m-d");
    

    IT will output the date that is 2 years and 5 months after January 1, 2020. In this case, it would output 2022-06-01.

    DateInterval Format Components

    P - Starts the period

    Date Components

    Y - Years (e.g., 2Y for 2 years)

    M - Months (e.g., 5M for 5 months)

    D - Days (e.g., 10D for 10 days)