Search code examples
laravelphp-carbon

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

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