Search code examples
phplaravelphp-carbon

generate credit card with +6 month expire


I have laravel project part of my code adds months and year in a database for generated cards the expiration month should be +6 months after generating the card

I have this code

        "year" => now()->format("Y"),
        "month" => now()->addMonths(6)->format("m"),

the problem is for the current month august I get the year 2022 and month 02 because this code gets month aug=8+6 =14

how to fix this?

for example for the august 2022 sample, I should get 02/2023


Solution

  • The solution is pretty simple in this case :D

    $date = now()->addMonths(6);
    
    "year" => $date->format('Y'),
    "month" => $date->format("m"),
    

    If the month you are creating it is January 2022, the date will hold July and 2022.

    If the month you are creating it is September 2022,the date will hold the March and 2023