如何获得laravel中碳日期的季度数

时间:2018-05-10 04:45:52

标签: php laravel laravel-5.5 php-carbon

我正在构建一个图表,其中包含时间线作为财务季度,所以我想要标签如下:

Dates between April-June as Q1
Dates between July-September as Q2
Dates between October-December as Q3
Dates between January-March as Q4

例如,我必须采用前6个月的标签,所以现在我将标签放置为

for($i=0; $i<6; $i++)
{
    $labels[] = Carbon::now()->startOfMonth()->subMonths($i)->format('M Y');
}

我怎样才能实现这个目标

2 个答案:

答案 0 :(得分:0)

HTTP_REFERER

也许那样?

答案 1 :(得分:0)

您可以使用Carbon的quarter方法

$dt = \Carbon\Carbon::now(); // 5/10/2018 -> is on 2nd quarter of the year
$dt->quarter; // 2 =>  Q2

所以你可以做到

for($i=0; $i<6; $i++)
{
    $dt = \Carbon\Carbon::now()->startOfMonth()->subMonths($i);
    $labels['Q'.$dt->quarter][] = $dt->format('M Y');
}
dd($labels);

// result:
array:3 [▼
  "Q2" => array:2 [▼
    0 => "May 2018"
    1 => "Apr 2018"
  ]
  "Q1" => array:3 [▼
    0 => "Mar 2018"
    1 => "Feb 2018"
    2 => "Jan 2018"
  ]
  "Q4" => array:1 [▼
    0 => "Dec 2017"
  ]
]