Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'));
like image 219
rap-2-h Avatar asked Oct 16 '17 13:10

rap-2-h


People also ask

How do you set the date on Carbon?

Carbon also allows us to generate dates and times based on a set of parameters. For example, to create a new Carbon instance for a specific date use the Carbon::createFromDate() method, passing in the year, month, day, and timezone, as in the following example. <? php require __DIR__ .

How do you find the date on a Carbon object?

You can use array_map to produce this kind of result: $dates_formatted = array_map(function($entry) { // transform the Carbon object to something like 'Dec 25, 1975' return $entry['date']->toFormattedDateString(); }, $dates);

What is Nesbot Carbon?

An API extension for DateTime that supports 281 different languages.


1 Answers

I believe setISODate takes care of this:

$date = Carbon::now();
$date->setISODate(2017,3);
echo $date->startOfWeek();
like image 144
Daan Avatar answered Oct 12 '22 05:10

Daan