Forum

Get all the names of days in accordance to today's day with carbon

Hey, If my current day is Tue, then I want the output to be Mon, Sun, Sat, Fri, Thu, Wed. i.e name of the day for past 6 days. I am able to get the current day name with the following code. (But it's still not is short form)

$day = Carbon::now()->format( 'l' );

I was wondering how to get this done.

saurabh
saurabh
  • 1 month ago

You can create a for loop like the one below and loop over Carbon subtracting the date based on the current iteration. This will add each of the days into a$day variable, but you can mix this up as needed.

for ($i = 1; $i < 7; $i++) {
    $day[] = Carbon::now()->subDays($i)->format('D');
}