I have an array like so:
1 => array:7 [▼
"id" => "4"
"order_code" => "BS-ORD-000000004"
"order_type" => "Mixed - Subscription"
"subscription_months" => "4"
"subscription_start_month" => Carbon {#714 ▼
+"date": "2016-11-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"subscription_end_month" => Carbon {#723 ▼
+"date": "2017-03-04 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"monthName" => "November"
]
2 => array:7 [▼
"id" => "4"
"order_code" => "BS-ORD-000000004"
"order_type" => "Mixed - Subscription"
"subscription_months" => "4"
"subscription_start_month" => Carbon {#721 ▼
+"date": "2016-11-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"subscription_end_month" => Carbon {#717 ▼
+"date": "2017-03-04 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"monthName" => "December"
]
3 => array:7 [▼
"id" => "4"
"order_code" => "BS-ORD-000000004"
"order_type" => "Mixed - Subscription"
"subscription_months" => "4"
"subscription_start_month" => Carbon {#729 ▼
+"date": "2016-11-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"subscription_end_month" => Carbon {#730 ▼
+"date": "2017-03-04 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"monthName" => "January"
]
4 => array:7 [▼
"id" => "4"
"order_code" => "BS-ORD-000000004"
"order_type" => "Mixed - Subscription"
"subscription_months" => "4"
"subscription_start_month" => Carbon {#732 ▼
+"date": "2016-11-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"subscription_end_month" => Carbon {#733 ▼
+"date": "2017-03-04 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"monthName" => "February"
]
Now what I want is to append a year to the monthName
key after adding the months from subscription_start_month
key.
Example of what I want:
2 => array:7 [▼
"id" => "4"
"order_code" => "BS-ORD-000000004"
"order_type" => "Mixed - Subscription"
"subscription_months" => "4"
"subscription_start_month" => Carbon {#721 ▼
+"date": "2016-11-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"subscription_end_month" => Carbon {#717 ▼
+"date": "2017-03-04 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"monthName" => "December, 2016" // <-- The current year
]
3 => array:7 [▼
"id" => "4"
"order_code" => "BS-ORD-000000004"
"order_type" => "Mixed - Subscription"
"subscription_months" => "4"
"subscription_start_month" => Carbon {#729 ▼
+"date": "2016-11-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"subscription_end_month" => Carbon {#730 ▼
+"date": "2017-03-04 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
"monthName" => "January, 2017" // <-- The next year
]
I have the tried the following code, but it does not work as expected.
$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++;
}
UPDATE 1:
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 the array should look 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
]
]
How can I achieve that ? Kindly help me out..