简体   繁体   中英

How to get results of the weekend laravel

I'm trying to get results of the weekend in laravel. I would like to show all the events of the weekend during all the week. So I would like to show all results from monday to sunday, but just weekend events. I have:

$friday = Carbon::parse('this friday')->toDateTimeString();
$sunday = Carbon::parse('this sunday')->toDateTimeString();
if (Carbon::now()->gt(Carbon::parse('this friday'))) {
        $friday = Carbon::parse('last friday')->toDateTimeString();
}
$events = Event::where([
        ['date', '>=', $friday],
        ['date', '<=', $sunday]
    ])
        ->orderBy('date', 'asc')
        ->get();

But when it's satuday this doesn't work. I don't know how to do it. Any idea? Thanks!

Firstly, you can use code: $dt = Carbon::now(); $dt->isWeekend(); $dt = Carbon::now(); $dt->isWeekend(); to check is weekend or not, then calculate the $friday date and $sunday date.

I also need to show events filtered by week and weekend:

$now = Carbon::now();


//This weekend's datarange
$starOftWeekend = $now->endOfWeek()->subDays(2)->format('Y-m-d H:i');
$endOfWeekend   = $now->endOfWeek()->format('Y-m-d H:i');


//Weekly date range
$startOfWeek = $now->startOfWeek()->format('Y-m-d H:i'); 
$endOfWeek = $now->endOfWeek()->format('Y-m-d H:i'); 

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