如何在 Laravel 5.1 中获取上周的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35307722/
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
How to get data of previous week in Laravel 5.1?
提问by Amrinder Singh
I want to get data inserted in previous week in Laravel 5.1. Currently I am using following query:
我想在 Laravel 5.1 中插入上周的数据。目前我正在使用以下查询:
$AgoDate=\Carbon\Carbon::now()->subWeek()->format('Y-m-d'); // returns 2016-02-03
$NowDate=\Carbon\Carbon::now()->format('Y-m-d'); // returns 2016-02-10
$query->whereBetween('created_on', array($AgoDate,$NowDate));
This returns last 7 days data, but I need last week's data, which means it should return data from 31st Jan to 6th Feb, i.e. the previous week.
这将返回过去 7 天的数据,但我需要上周的数据,这意味着它应该返回从 1 月 31 日到 2 月 6 日的数据,即前一周。
Note: in my case Week starts from Sunday and ends on Saturday.
注意:就我而言,周从周日开始,周六结束。
回答by codisfy
EDIT: adjusting to OP's question
编辑:适应 OP 的问题
For ago date use
对于前日期使用
$currentDate = \Carbon\Carbon::now();
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();// gives 2016-01-31
Similarly you can get your nowDate
同样你可以得到你的 nowDate
$currentDate = \Carbon\Carbon::now();
$nowDate = $currentDate->subDays($currentDate->dayOfWeek + 1); // gives 2016-02-06
to adjust the weekstart(sunday/monday) I think you can add/subtract some days to dayOfWeek e.g $currentDate->subDays($currentDate->dayOfWeek + 1 /* or -1 */);
调整weekstart(sunday/monday) 我想你可以添加/减去dayOfWeek 例如 $currentDate->subDays($currentDate->dayOfWeek + 1 /* or -1 */);
EDIT : as mentioned in the comments you can also use startOfWeek()
method instead of subDays()
if it works directly for you.
编辑:如评论中所述,您也可以使用startOfWeek()
方法,而不是subDays()
直接为您工作。