Displaying of month and it's year based on condition

馋奶兔 提交于 2019-12-25 07:59:12

问题


In my Laravel 5.1 application, I have a feature of Subscription. It is working to a certain extent, but not what I am looking for..

Scenario: A user makes a subscription on October 2016 for 6 months.. So, the array will look something like this:

[
    "subscription_months" => 6
    "subscription_start_month" => "01-10-2016" // <-- will be carbon instance
    "subscription_end_month => "31-03-2017" // <-- will be carbon instance
    "monthName" => "October"
]

The above array I am showing is only 1 month.. There will be 5 more elements in the array..

What I want is, I want to append the Year after monthName

So, it would look something like this.

[
    [
        "monthName" => "October, 2016" // <-- for 1st month
    ]

    [
        "monthName" => "November, 2016" // <-- for 2nd month
    ]

    [
        "monthName" => "December, 2016" // <-- for 3rd month
    ]

    [
        "monthName" => "January, 2017" // <-- for 4th month
    ]

    [
        "monthName" => "February, 2017" // <-- for 5th month
    ]

    [
        "monthName" => "March, 2017" // <-- for 6th month
    ]
]

The code that I have tried so far is:

$startMonthForMixed = $invoice->subscription_start_date->month;

for($i = 0; $i < $invoice->subscription_months; $i++) {
    $convertedInvoices[] = [
        'id' => $invoice->id,
        'order_code'               => $invoice->order_code,
        'order_type'               => 'Mixed - Subscription',
        'subscription_months'      => $invoice->subscription_months,
        'subscription_start_month' => $invoice->subscription_start_date,
        'subscription_end_month'   => $invoice->subscription_end_date,
        'monthName'                => date("F", mktime(0, 0, 0, $startMonthForMixed, 01)) . ", " . Carbon::now()->addYear()
     ];
     $startMonthForMixed++;
 }

Can anybody help out with this ? Thanks in advance.


回答1:


The code you have tried with will always use the current year, so it would not work if the subscription period is in another year.

You could use this variation on your code, which makes use of some Carbon and DateTime methods:

// take copy of start date
$subscription_month = $invoice->subscription_start_date->copy();

for($i = 0; $i < $invoice->subscription_months; $i++) {
    $convertedInvoices[] = [
        'id' => $invoice->id,
        'order_code'               => $invoice->order_code,
        'order_type'               => 'Mixed - Subscription',
        'subscription_months'      => $invoice->subscription_months,
        'subscription_start_month' => $invoice->subscription_start_date,
        'subscription_end_month'   => $invoice->subscription_end_date,
        'monthName'                => $subscription_month->format("F, Y")
    ];
    // move to first date of following month
    $subscription_month->modify('first day of next month');
}



回答2:


You can do a cycle for the number of months, using the add method of Carbon object to add 1 month. Then, formatting is just a matter of calling format method, as trincot already suggested



来源:https://stackoverflow.com/questions/38169733/displaying-of-month-and-its-year-based-on-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!