6

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

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

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

4

5 回答 5

8

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

$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();
于 2015-04-06T15:28:48.223 回答
3

我想我已经解决了:

...
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;
...

如果有的话,请告诉我一种更好的方法,非常感谢您的帮助。

于 2015-04-06T15:04:15.127 回答
1

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

$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 */
于 2020-07-21T07:40:50.633 回答
0
  • 以当前日期作为输入查找当前季度的开始和结束日期,并以自定义格式格式化日期。

**

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

**

于 2020-09-11T05:01:58.513 回答
0

返回给定日期的季度。

$date = Carbon::parse($timestamp);
$date->quarter;
于 2021-12-29T03:14:45.403 回答