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

saurabh posted 1 year ago

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.

rowright replied 1 year 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');

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us