Home > database >  Carbon get quarter of the year
Carbon get quarter of the year

Time:12-08

I want to get my data quarter based so for each year I will have 4 variable like pastYearsQOne pastYearsQTwo pastYearsQThree pastYearsQFour.

My question is how can I get quarters of the year?

code

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

If still didn't get it! here is the logic,

Final result will be like:

$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)

CodePudding user response:

startOfQuarter and endOfQuarter quarter are useful, along with month function.

Please try using the below code.

$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();

CodePudding user response:

you can add a variable for current year Carbon::today() then recursively add 3 months for all quarter Carbon::addMonths(3)

CodePudding user response:

Is this what you mean ?

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();
}

Here I also change the query based on the data you want to retrieve.

  • Related