Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon: Get start and end date of week when knowing week in year and year

Carbon provides the function weekOfYear to get the week of the year as integer. However I need to go the other way round to get the a date based on the year + the week of the year.

Carbon::now()->weekOfYear(); // todays week of the year

E.g.

  • year: 2016
  • week of year: 42

As a result i need the start and end date of this given week. However i cannot find a fitting function in the Carbon docs

like image 848
Frnak Avatar asked Oct 20 '16 13:10

Frnak


People also ask

How do you find a carbon current date?

$current_date_time = new DateTime("now"); $user_current_date = $current_date_time->format("Y-m-d"); to get toDay date.


1 Answers

Carbon is a wrapper for PHP's DateTime, so you can use setISODate:

$date = Carbon::now(); // or $date = new Carbon();
$date->setISODate(2016,42); // 2016-10-17 23:59:59.000000
echo $date->startOfWeek(); // 2016-10-17 00:00:00.000000
echo $date->endOfWeek(); // 2016-10-23 23:59:59.000000
like image 160
aynber Avatar answered Nov 14 '22 16:11

aynber