Laravel Carbon 查找上个月的同一天

Laravel Carbon find same day of week in previous month

如果今天的日期是 Wednesday Oct 2nd 2019,我正在尝试查找与上个月相同的星期几。我需要检索 Wednesday Sept 4th 2019

我正在使用 Carbon 并尝试了 subDays(30)subMonth(1),但显然 return 不是同一天。

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

此代码按预期工作,但我无法弄清楚如何让它根据前一个月找到一周中的同一天。

不太清楚您要做什么,但我会试一试。如果减去一个月,然后转到下一个匹配的工作日呢?

$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.

这是否满足您的需求?