Laravel 使用 carbon 获取当前季度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29473369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel using carbon to get current quarter
提问by imperium2335
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.
如何使用 Carbon 确定当前季度?即我想掌握季度开始的日期和结束的日期。
I tried the intuitive echo new Carbon('this quarter');
way which doesn't work, but I guess they don't have one for quarters.
我尝试了echo new Carbon('this quarter');
不起作用的直观方式,但我猜他们没有一个季度。
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.
但是现在我正在为如何获得上一季度的开始和结束日期而苦苦挣扎。
回答by user1669496
You can use the firstOfQuarter
and lastOfQuarter
methods for determining the beginning and end dates of a quarter...
您可以使用firstOfQuarter
和lastOfQuarter
方法来确定一个季度的开始和结束日期...
$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();
回答by imperium2335
I think I have solved it:
我想我已经解决了:
...
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;
...
Please let me know a nicer way to do this if there is one, your help is much appreciated.
如果有更好的方法,请告诉我,非常感谢您的帮助。