根据条件显示月份和年份 [英] Displaying of month and it's year based on condition

查看:136
本文介绍了根据条件显示月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Laravel 5.1应用程序中,我有一个订阅功能。它在一定程度上正在工作,但不是我正在寻找的。

场景:用户在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆