'Laravel Carbon get start + end of last week?

I have laravel carbon for get start + end of current week :

$startofcurrentweek =Carbon::now()->startOfWeek(); //2020-02-17 00:00:00
$endofcurrentweek =Carbon::now()->endOfWeek(); //2020-02-23 23:59:59

How To get Start of Last Week using carbon ,... So i can get,

$startoflasttweek  = 2020-02-10 00:00:00
$endoflastweek  = 2020-02-16 23:59:59


Solution 1:[1]

You can subtract 7 days to the start of current week or subtract 7 days from now and get the start of the week.

$startOfCurrentWeek = Carbon::now()->startOfWeek(); 

$startOfLastWeek  = $startOfCurrentWeek->copy()->subDays(7);
$startOfLastWeek  = Carbon::now()->subDays(7)->startOfWeek();

And the same to get the end of the last week.

Solution 2:[2]

The answer of Porloscerros is correct but need a little bit fix:

$startOfCurrentWeek = Carbon::now()->startOfWeek(); 

$startOfLastWeek  = $startOfCurrentWeek->copy()->subDays(7);

$startOfLastWeek  = Carbon::now()->subDays(7)->startOfWeek()->endOfDay();

with ->endOfDay() This will return 23:59:59 9999.99 instead of 00:00:00 (beginning of the day)

or you can use ->endOfWeek() for the same result

Solution 3:[3]

Similar to answers above but more clear:

$startOfLastWeek = Carbon::now()->subDays(7)->startOfWeek();
$endOfLastWeek = Carbon::now()->subDays(7)->endOfWeek();
  1. It substracts 7 days from the current time (which goes to the last week) and startOfWeek gets the starting date and time of that week.
  2. It substracts 7 days from the current time (which goes to the last week) and endOfWeek gets the ending date and time of that week.

Result/Example?

If current time is 2022-04-27 00:00:00.0 UTC (+00:00) then it results as follows:

  1. 2022-04-18 00:00:00.0 UTC (+00:00)
  2. 2022-04-24 23:59:59.999999 UTC (+00:00)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MaartenDev
Solution 2
Solution 3