简体   繁体   中英

How to retrive all the data based on current month of all the weeks in laravel?

I need to retrieve all records based on week.

Data could be like this:

array(
numberofweaks:5
weak_1 = [2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42]
weak_2 = [2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42]
weak_3 = [2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42]
weak_4 = [2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42]
weak_5 = [2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42, 2019-11-21 22:00:42]
)

A lot of references tried but none are working.

Tried this, not working:

$monthOfWeaks = DB::table("UserDetails")
    ->whereBetween('creationdate', [Carbon::now()->subWeek(4)->format("Y-m-d H:i:s"), Carbon::now()])
    ->get();

Please share your reference!!

First of all, if by "weak" you are referring to the period of 7 days, the word is actually "week".

Then, you are using the method subWeek(4) on Carbon. This method doesn't receive any arguments (because it should be just one week), if you want to substract many weeks, use subWeeks($int) instead. Check the docs :

Carbon::now()->subWeeks(4);

Finally, you don't need to format the date, just pass the instance. So your query should look like this:

$data = DB::table("UserDetails")
    ->whereBetween('creationdate', [now()->subWeeks(4), now()])
    ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM