如何使用碳来确定当前季度?我想知道季度开始的日期和结束的日期。
我尝试了直觉的echo new Carbon('this quarter');方法,但我想他们没有一个季度。
我想出来了,我想了:
$query->where(DB::raw('QUARTER(FT.created_at)'), Carbon::now()->quarter);
$query->where(DB::raw('YEAR(FT.created_at)'), '=', Carbon::now()->year);但是现在我在为如何获得最后一个季度的开始和结束日期而挣扎。
发布于 2015-04-06 15:28:48
您可以使用firstOfQuarter和lastOfQuarter方法来确定季度的开始日期和结束日期.
$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();发布于 2015-04-06 15:04:15
我想我已经解决了:
...
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;
...如果有,请让我知道一个更好的方法,你的帮助是非常感谢的。
发布于 2020-07-21 07:40:50
为了在上面的答案中添加更多内容,应该使用的实际方法如下:
$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)以下不完全正确:
$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 */https://stackoverflow.com/questions/29473369
复制相似问题