Search code examples
phpdatetimephp-carbon

Set a date by weeknumber with carbon


I know I can get the week number with Carbon by calling $my_date->weekOfYear. Is there a way to set a date by week number?

I mean is there something for initializing a date to the first day of the 3rd week of 2017 year?

I can initialize a date to the start of the year, I think it's a good start but I don't know what to do next:

$a = (new \Carbon\Carbon('2017-01-01'));

Solution

  • I believe setISODate takes care of this:

    $date = Carbon::now();
    $date->setISODate(2017,3);
    echo $date->startOfWeek();