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

3

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

Graecize answered 20/10, 2016 at 13:35 Comment(0)
R
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
Robillard answered 20/10, 2016 at 13:50 Comment(3)
use $date->copy()->startOfWeek() and $date->copy()->endOfWeek() in case you receive the same date object as startOfWeek.Windowsill
This will fail with dates like 2021-01-01. See my answer for a better approachLamee
@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 - 53Robillard
L
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

Lamee answered 27/8, 2021 at 7:2 Comment(0)
K
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);
 }
Kaifeng answered 1/9, 2021 at 7:29 Comment(2)
Please explain your solution. Answers which do not have an explanation and are only code get flagged as low effort.Allheal
Please add further details to expand on your answer, such as working code or documentation citations.Gunwale

© 2022 - 2024 — McMap. All rights reserved.