碳获取季度

Carbon get quarter of the year

我想获取基于季度的数据,因此每年我都会有 4 个变量,例如 pastYearsQOne pastYearsQTwo pastYearsQThree pastYearsQFour.

我的问题是我怎样才能得到一年中的季度?

code

$pastYearsQOne = Project::orderby('id', 'desc')->whereDate(
  'created_at', '<=', now()->subYear(1)->startOfQuarter()->toDateTimeString()
                                         ^^^^^^^^^^^^^^^^
)->count();

如果还是没听懂!这是逻辑,

最终结果如下:

$pastYearsQOne = data of: 2020 (year) -> 01~03 (months)

$pastYearsQTwo = data of: 2020 (year) -> 04~06 (months)

$pastYearsQThree = data of: 2020 (year) -> 07~09 (months)

$pastYearsQFour = data of: 2020 (year) -> 10~12 (months)

startOfQuarterendOfQuarter quarter 以及 month 函数很有用。

请尝试使用以下代码。

$startOfQ1 = Carbon::now()->subYear()->month(1)->startOfQuarter();
$endOfQ1 = Carbon::now()->subYear()->month(1)->endOfQuarter();

$startOfQ2 = Carbon::now()->subYear()->month(4)->startOfQuarter();
$endOfQ2 = Carbon::now()->subYear()->month(4)->endOfQuarter();

$startOfQ3 = Carbon::now()->subYear()->month(7)->startOfQuarter();
$endOfQ3 = Carbon::now()->subYear()->month(7)->endOfQuarter();

$startOfQ4 = Carbon::now()->subYear()->month(10)->startOfQuarter();
$endOfQ4 = Carbon::now()->subYear()->month(10)->endOfQuarter();

您可以为当年添加一个变量Carbon::today() 然后递归地为所有季度添加 3 个月 Carbon::addMonths(3)

你是这个意思吗?

public function index()
{
    $now = Carbon::now()->subYears(1)->format('Y-m-d H:i:s');

    $pastQuarterOneStart = Carbon::parse($now)->subQuarters(3)->startOfQuarter()->format('Y-m-d H:i:s'); // 2020-01-01 00:00:00
    $pastQuarterOneEnd = Carbon::parse($now)->subQuarters(3)->endOfQuarter()->format('Y-m-d H:i:s'); // 2020-03-31 23:59:59

    $pastQuarterTwoStart = Carbon::parse($now)->subQuarters(2)->startOfQuarter()->format('Y-m-d H:i:s'); // 2020-04-01 00:00:00
    $pastQuarterTwoEnd = Carbon::parse($now)->subQuarters(2)->endOfQuarter()->format('Y-m-d H:i:s'); // 2020-06-30 23:59:59

    $pastQuarterThreeStart = Carbon::parse($now)->subQuarter(1)->startOfQuarter()->format('Y-m-d H:i:s'); // 2020-07-01 00:00:00
    $pastQuarterThreeEnd = Carbon::parse($now)->subQuarter(1)->endOfQuarter()->format('Y-m-d H:i:s'); // 2020-09-30 23:59:59

    $pastQuarterFourStart = Carbon::parse($now)->startOfQuarter()->format('Y-m-d H:i:s'); // 2020-10-01 00:00:00
    $pastQuarterFourEnd = Carbon::parse($now)->endOfQuarter()->format('Y-m-d H:i:s'); // 2020-12-30 23:59:59


    // Get 2020-01-01 00:00:00 to 2020-03-31 23:59:59
    $pastYearsQOne = Project::orderby('id', 'desc')->whereBetween(
        'created_at',
        [
            $pastQuarterOneStart,
            $pastQuarterOneEnd
        ]
    )->count();

    // Get 2020-04-01 00:00:00 to 2020-06-31 23:59:59
    $pastYearsQTwo = Project::orderby('id', 'desc')->whereBetween(
        'created_at',
        [
            $pastQuarterTwoStart,
            $pastQuarterTwoEnd
        ]
    )->count();

    // Get 2020-07-01 00:00:00 to 2020-09-31 23:59:59
    $pastYearsQThree = Project::orderby('id', 'desc')->whereBetween(
        'created_at',
        [
            $pastQuarterThreeStart,
            $pastQuarterThreeEnd
        ]
    )->count();

    // Get 2020-10-01 00:00:00 to 2020-12-31 23:59:59
    $pastYearsQFour = Project::orderby('id', 'desc')->whereBetween(
        'created_at',
        [
            $pastQuarterFourStart,
            $pastQuarterFourEnd
        ]
    )->count();
}

这里我也根据你要检索的数据更改查询。