首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用碳获得当前季度

利用碳获得当前季度
EN

Stack Overflow用户
提问于 2015-04-06 14:34:15
回答 5查看 14.5K关注 0票数 7

如何使用碳来确定当前季度?我想知道季度开始的日期和结束的日期。

我尝试了直觉的echo new Carbon('this quarter');方法,但我想他们没有一个季度。

我想出来了,我想了:

代码语言:javascript
复制
$query->where(DB::raw('QUARTER(FT.created_at)'), Carbon::now()->quarter);
$query->where(DB::raw('YEAR(FT.created_at)'), '=', Carbon::now()->year);

但是现在我在为如何获得最后一个季度的开始和结束日期而挣扎。

EN

回答 5

Stack Overflow用户

发布于 2015-04-06 15:28:48

您可以使用firstOfQuarterlastOfQuarter方法来确定季度的开始日期和结束日期.

代码语言:javascript
复制
$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();
票数 8
EN

Stack Overflow用户

发布于 2015-04-06 15:04:15

我想我已经解决了:

代码语言:javascript
复制
...
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;
...

如果有,请让我知道一个更好的方法,你的帮助是非常感谢的。

票数 4
EN

Stack Overflow用户

发布于 2020-07-21 07:40:50

为了在上面的答案中添加更多内容,应该使用的实际方法如下:

代码语言:javascript
复制
$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)

以下不完全正确:

代码语言:javascript
复制
$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 */
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29473369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档