Laravel Carbon, retrieve today's date with weekday?

Viewed 57479

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'));
}
4 Answers

If you're in an English-speaking locale, you can get that short weekday format by doing some processing on Carbon's l format, which returns weekday names:

strtoupper(substr($today->format('l'), 0, 2)); // Returns 'MO', 'TU', etc

It can be even shorter if you have access to Carbon 2 (available on 2018-08-31):

strtoupper($today->isoFormat('dd'));

This works perfect

    Carbon::parse($invoice->date)->format('l d M Y');

$weekday = Carbon::parse($today)->format('1');

Related