Laravel using carbon to get current quarter

imperium2335 picture imperium2335 · Apr 6, 2015 · Viewed 8.8k times · Source

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.

Answer

user1669496 picture user1669496 · Apr 6, 2015

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