1

I am trying to find the same day of week from last month if today's date is Wednesday Oct 2nd 2019. I need to retrieve Wednesday Sept 4th 2019.

I am using Carbon and have tried subDays(30) and subMonth(1) but that obviously doesn't return the same week day.

SalesLogs::loadByDate(Carbon::now()->subMonth(1));

This code works as expected, however I am unable to work out how to make it find the same day of the week based on the prior month.

herrbischoff
  • 3,294
  • 1
  • 29
  • 50

1 Answers1

0

It's not super clear what you are trying to do, but I will take a shot at it. What about if you subtract a month, and then go to the next matching weekday?

$weekday = now()->dayOfWeek;
SalesLogs::loadByDate(now()->subMonth(1)->next($weekday));

Note: you can take advantage of Laravel's handy now() helper function, which is equal to Carbon::now(), but saves you from having to import Carbon.

Does that get you what you need?

Vince
  • 3,207
  • 1
  • 17
  • 28