Laravel Carbon, retrieve today's date with weekday?
Asked Answered
G

6

31

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'));
}
Grace answered 5/7, 2017 at 10:35 Comment(0)
E
52

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];
Edgy answered 5/7, 2017 at 10:49 Comment(4)
is 0 ALWAYS sunday??? cause not everyones week starts with sundayBanebrudge
Carbon::now()->dayOfWeekIso should return as 1=>monday 7=>sundayBassorilievo
"I'm not sure that Carbon has such formatting" => Carbon::now()->englishDayOfWeek will return the week days as Sunday or MondayNeoplatonism
Be aware that Carbon's day numbering is different to MySQL's WEEKDAY function, which uses Monday = 0, Sunday = 6.Declinometer
F
25

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'));
Family answered 5/9, 2018 at 20:40 Comment(1)
this answer is far betterLandmeier
B
5

2024 Update

Carbon seems to have a lot more methods to offer now.
For this scenario something as simple as this will work:

strtoupper(Carbon::now()->minDayName);

If you are not on an english locale you can add locale('en') to it.
But there are also a lot more things you can make use of.
Check out the getters section at the docs for more info.

$dt = Carbon::now();

// dayOfWeek returns a number between 0 (sunday) and 6 (saturday)
var_dump($dt->dayOfWeek);                                  // int(5)
// dayOfWeekIso returns a number between 1 (monday) and 7 (sunday)
var_dump($dt->dayOfWeekIso);                               // int(5)
var_dump($dt->englishDayOfWeek);                           // string(6) "Friday"
var_dump($dt->shortEnglishDayOfWeek);                      // string(3) "Fri"
var_dump($dt->locale('de')->dayName);                      // string(7) "Freitag"
var_dump($dt->locale('de')->shortDayName);                 // string(3) "Fr."
var_dump($dt->locale('de')->minDayName);  
Bassorilievo answered 29/9, 2022 at 9:31 Comment(0)
U
2

This works perfect

    Carbon::parse($invoice->date)->format('l d M Y');
Unweave answered 10/5, 2022 at 13:28 Comment(0)
D
1
$dayOfWeek = Carbon::parse($delivery->delivery_date)->dayOfWeek;
Directional answered 12/9, 2023 at 8:48 Comment(1)
Please add some more details on how this answers the question and how this differs from existing answers. Code-only answers are not the most useful ones.Phosphoroscope
C
-2

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

Crawford answered 15/11, 2020 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.