【问题标题】:Display month along with its year显示月份及其年份
【发布时间】:2016-11-04 17:57:44
【问题描述】:

我有一个这样的数组:

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"
]

现在我想要的是在添加来自 subscription_start_month 键的月份之后,在 monthName 键上附加一年。

我想要的示例:

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
]

我已经尝试了以下代码,但它没有按预期工作。

$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++;
 }

更新 1:

场景:用户在 2016 年 10 月订阅了 6 个月。因此,数组将如下所示:

[
    "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"
]

我展示的上述数组只有 1 个月.. 数组中还有 5 个元素..

我想要的是,我想在monthName 之后附加Year..

所以数组应该是这样的:

[
    [
        "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
    ]
]

我怎样才能做到这一点?请帮帮我..

【问题讨论】:

  • 不确定要添加哪一年。开始日期的年份还是结束日期的年份?
  • @MassimilianoArione 根据年份,将subscription_months 添加到subscription_end_month..
  • @MassimilianoArione 我已经添加了 UPDATE 1 部分,请看一下..

标签: php arrays laravel-5.1 php-carbon


【解决方案1】:

鉴于您在其中有 Carbon 对象,您可以使用 year 属性:

foreach($data as $i => $row) {
    $data[$i]["monthName"] .= ", " . $row["subscription_start_month"]->year;
}

或者,您可以使用 Carbon 的 format 从头开始​​格式化日期:

foreach($data as $i => $row) {
    $data[$i]["monthName"] = $row["subscription_start_month"]->format('F, Y');
}

【讨论】:

  • 我只会得到subscription_start_month 的年份。我需要的与您提供给我的解决方案略有不同。我添加了 UPDATE 1 ,看看它..你会明白我在找什么..
  • 这是您问题的一个重要变化。请为此提出一个新问题。
  • 查看链接stackoverflow.com/questions/38169733/…我已经按照你的建议提出了这个问题。请帮助我。
  • 我在您的其他问题上发布了answer。也许您可以从这个问题中删除该后续问题?如果这里的答案回答了最初的问题,你能告诉我吗? ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 2011-06-11
  • 1970-01-01
  • 2018-03-02
相关资源
最近更新 更多