Laravel using carbon to get current quarter
Asked Answered
P

6

8

How can I use Carbon to determine the current quarter? I.e. I would like to get hold of the date when the quarter started and the date when it ends.

I tried the intuitive echo new Carbon('this quarter'); way which doesn't work, but I guess they don't have one for quarters.


I figured it out, I did:

$query->where(DB::raw('QUARTER(FT.created_at)'), Carbon::now()->quarter);
$query->where(DB::raw('YEAR(FT.created_at)'), '=', Carbon::now()->year);

But now I am struggling with how to get the start and end date of the last quarter.

Pairoar answered 6/4, 2015 at 14:34 Comment(6)
What date would you want it to return? Or do you just want to get the current quarter number of the created date?Gusta
The date as returned by Carbon::now()->toDateString()Pairoar
Do you want just to check whether the date is in last quarter?Accelerometer
@Accelerometer Yes that's right, it's for a filter I'm making.Pairoar
@Pairoar You are using QUARTER(FT.created_at), So you might don't need to be worry about the start or end date of the quarter anymore. Why don't you just pass 4 into the query?Accelerometer
@Accelerometer The problem I am having is when it crosses a year. I am in the first quarter, and filter to show the last quarter, that needs to constrain the date by year as well as quarter (4).Pairoar
A
8

You can use the firstOfQuarter and lastOfQuarter methods for determining the beginning and end dates of a quarter...

$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();
Atropine answered 6/4, 2015 at 15:28 Comment(0)
P
4

I think I have solved it:

...
case 9:
                    $a = Carbon::now();
                    $a->month($a->month-3);
                    $lastQuarter = $a->quarter;
                    $query->where(DB::raw('QUARTER(FT.created_at)'), $lastQuarter);
                    $query->where(DB::raw('YEAR(FT.created_at)'), $a->year);
                    break;
...

Please let me know a nicer way to do this if there is one, your help is much appreciated.

Pairoar answered 6/4, 2015 at 15:4 Comment(0)
M
1

Just to add more to the answer above, the actual methods that should be use is the following methods:

$date = new \Carbon\Carbon('-3 months'); // for the last quarter requirement
$date->startOfQuarter(); // the actual start of quarter method
$date->endOfQuarter(); // the actual end of quarter method (with time 23:59:59.999999)

the following are not exactly correct:

$date->firstOfQuarter(); /* use this method when you wish to get the first
                            occurrence of a given day in the current quarter, its 
                            fallback works similar to the startOfQuarter() method */
$date->lastOfQuarter(); /* this is where the problem located, unlike the
                           endOfQuarter() method, this method return the start of a
                           day (with time 00:00:00.000000) (because the method is
                           designed to get the last occurrence of a given day in the
                           current quarter */
Mcmahan answered 21/7, 2020 at 7:40 Comment(0)
I
0
  • To find the current start and end date of the quarter with current date as input and format the date with in custom format.

**

$yyyymm = current date(); $date = new \Carbon\Carbon($yyyymm);
$ProfileData['date_from'] = $date->firstOfQuarter()->format('Ymd');
$ProfileData['date_to'] = $date->endOfQuarter()->format('Ymd');

**

Invisible answered 11/9, 2020 at 5:1 Comment(0)
D
0

Return the quarter for a given date.

$date = Carbon::parse($timestamp);
$date->quarter;
Daddy answered 29/12, 2021 at 3:14 Comment(1)
For me: > $quarter = Carbon\Carbon::parse($myModel->end_date)->quarter; worked like a charm.Relate
B
0

Return the quarter First And Last Date

$firstOfQuarter = Carbon::now()->firstOfQuarter()->format('Y-m-d');

$endOfQuarter = Carbon::now()->endOfQuarter()->format('Y-m-d');
Berbera answered 16/8, 2023 at 5:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.