如何使用 Carbon 确定当前季度? IE。我想知道季度开始的日期和结束的日期。
我尝试了直观的
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);
但现在我正在努力解决如何获取上季度的开始和结束日期。
您可以使用
firstOfQuarter
和 lastOfQuarter
方法来确定季度的开始和结束日期...
$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();
我想我已经解决了:
...
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;
...
如果有更好的方法,请告诉我,非常感谢您的帮助。
只是为了在上面的答案中添加更多内容,实际应该使用的方法是以下方法:
$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 */
**
$yyyymm = 当前日期(); $date = new \Carbon\Carbon($yyyymm);
$ProfileData['date_from'] = $date->firstOfQuarter()->format('Ymd');
$ProfileData['date_to'] = $date->endOfQuarter()->format('Ymd');
**
返回给定日期的季度。
$date = Carbon::parse($timestamp);
$date->quarter;
返回季度的第一个和最后一个日期
$firstOfQuarter = Carbon::now()->firstOfQuarter()->format('Y-m-d');
$endOfQuarter = Carbon::now()->endOfQuarter()->format('Y-m-d');