Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Carbon, retrieve today's date with weekday?

Tags:

I am using carbon to compare 2 dates with today's date, however I also have another field in a database called weekday which contains values like:

'MO' 'TU' 'WE'

So I don't only want to search and output by dates but also search by a weekday so:

public function show($id) {        $today = Carbon::now();     $weekday = //whatever carbon or something else has to retrieve today's day     $event = Event::with('businesses')        ->where('startdate', '<', $today->format('Y-m-d'))        ->where('endate', '>', $today->format('Y-m-d'))        //or where ('weekday') = $weekday?        ->get();     return view('events.showEvent', compact('event')); } 
like image 484
Przemek Wojtas Avatar asked Jul 05 '17 10:07

Przemek Wojtas


People also ask

How do you find the day of the week in Carbon?

$time = time(); $day_as_number = date('N', $time);


1 Answers

I'm not sure that Carbon has such formatting, but what you could do is get the wekkday from a map of days and the current week day constant:

$weekMap = [     0 => 'SU',     1 => 'MO',     2 => 'TU',     3 => 'WE',     4 => 'TH',     5 => 'FR',     6 => 'SA', ]; $dayOfTheWeek = Carbon::now()->dayOfWeek; $weekday = $weekMap[$dayOfTheWeek]; 
like image 66
thefallen Avatar answered Sep 29 '22 13:09

thefallen