Search code examples
php-carbon

How do i get the date from week of month and day of week with Carbon?


I have a date, I want to get from week of month and day of week of any month.

Example:

  • Date: 2021-04-26 (Monday)
  • Week of month: 5
  • Day of week: 1

I want to get the date of May by week of month and day of week. The date I want to get is "2021-05-24"

How can I do that with Carbon?


Solution

  • $month = 5;
    $weekOfMonth = 5;
    $dayOfWeek = 1;
    
    echo Carbon::parse("2021-$month-1")
        ->startOfWeek()
        ->addWeeks($weekOfMonth - 1)
        ->addDays($dayOfWeek - 1);