In this tutorial I will show you how to get week dates from week number and year. We will calculate ISO week number (1-53) and represent the start of the week “Monday”.
There is a setISODate() method with DateTime extension that accepts the year (4-digit year) and ISO week number.
Example of get week dates from week number and year
function getWeekDates($week, $year) {
$dates = array();
$dateTime = new DateTime();
$dateTime->setISODate($year, $week);
$dates[] = $dateTime->format('Y-m-d');
for($i=0; $i<6; $i++){ $dates[] = $dateTime->modify("+1 days")->format('Y-m-d');
}
return $dates;
}
$week=18;
$year=2018;
$weekDates = getWeekDates($week, $year);
print_r($weekDates);
The output of above code is following:
Array
(
[0] => 2018-04-30
[1] => 2018-05-01
[2] => 2018-05-02
[3] => 2018-05-03
[4] => 2018-05-04
[5] => 2018-05-05
[6] => 2018-05-06
)