根据条件显示月份和年份 [英] Displaying of month and it's year based on condition
问题描述
场景:用户在2016年10月进行订阅6个月..所以,阵列看起来像这样:
[
subscription_months=> 6
subscription_start_month=> 01-10-2016//< - 将是碳实例
subscription_end_month =>31-03-2017//< - 将是碳实例
monthName =>十月
]
上面显示的数组只有1个月..数组中还有5个元素。
我想要的是,我要附加年份
after monthName
所以,看起来像这样。
[
[
monthName=>2016年10月//< - 第一个月
]
[
monthName=>2016年11月//< - 第二个月
]
[
monthName=>2016年12月//< - 第3个月
]
[
monthName=>2017年1月 //< - 第4个月
]
[
monthName=>2017年2月//< - 第5个月
]
[
monthName=> 2017年3月//< - 第6个月
]
]
目前为止我已经尝试的代码是:
$ startMonthForMixed = $ invoice-> subscription_start_date-> month ; ($ 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 ++;
}
有人可以帮忙吗?感谢提前。
您可以使用 add
Carbon对象的方法添加1个月。然后,格式化只是调用格式
方法,因为trincot已经建议
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.
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
这篇关于根据条件显示月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!