13

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

3 Answers 3

44

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
3
  • 6
    use $date->copy()->startOfWeek() and $date->copy()->endOfWeek() in case you receive the same date object as startOfWeek.
    – f_i
    Jan 6, 2019 at 8:23
  • This will fail with dates like 2021-01-01. See my answer for a better approach Aug 27, 2021 at 6:52
  • 2
    @FaizanAkramDar The OP asked for finding the information starting from the Year and Week Number, not an actual date. For 2021-01-01, that would be 2020 - 53
    – aynber
    Aug 27, 2021 at 11:44
0
/**
 * @return array{0: \DateTime, 1: \DateTime}
 */
public static function getWeekDates(\DateTimeInterface $selectedDate): array
{
    $daysFromMonday = (int) $selectedDate->format('N') - 1;

    $fromDate = \DateTimeImmutable::createFromInterface($selectedDate)->modify("-{$daysFromMonday} days");

    $toDate = $fromDate->modify('+6 days');

    return [
        \DateTime::createFromImmutable($fromDate),
        \DateTime::createFromImmutable($toDate),
    ];
}

This returns date of Monday and Sunday (iso week number).

If you wish to know dates of Sunday and Saturday, you can easily modify the function (replace 'N' with 'w' in format) and remove -1

0

$WeekArray = array();

 $FirstDate = Carbon::now()->addYears(-2);
 $LastDate = Carbon::now()->addYears(2);

 while ($FirstDate <= $LastDate) {
    $WeekNumber = Carbon::parse($FirstDate)->weekOfYear;
    $WeekYear = Carbon::parse($FirstDate)->year;
    $StartOfWeek = Carbon::parse($FirstDate)->startOfWeek();
    $EndOfWeek = Carbon::parse($FirstDate)->endOfWeek(); 
    
    $WeekItem = new stdClass;
    $WeekItem->WeekNumber = $WeekNumber;
    $WeekItem->WeekYear = $WeekYear;
    $WeekItem->FirstDate = AppHelper::_DateFormatMysql($StartOfWeek);
    $WeekItem->LastDate = AppHelper::_DateFormatMysql($EndOfWeek);

    if (count($WeekArray) > 0) {
        if (collect($WeekArray)->where('WeekYear', $WeekItem->WeekYear)->where('WeekNumber', $WeekItem->WeekNumber)
                               ->where('FirstDate', $WeekItem->FirstDate)->where('LastDate', $WeekItem->LastDate)->count() == 0) 
        {
            array_push($WeekArray, $WeekItem);
        }
    }
    else {
        array_push($WeekArray, $WeekItem);
    }

    $FirstDate = Carbon::parse($FirstDate)->addDays(1);
 }
2
  • 1
    Please explain your solution. Answers which do not have an explanation and are only code get flagged as low effort.
    – cursorrux
    Sep 1, 2021 at 9:37
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Sep 1, 2021 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.