Last Updated: February 25, 2016
·
7.116K
· xwlee

Get dates from week number

Base on week number, get all dates in this week

Get month first week's timestamp

$month = 8;
$year = 2013;
$timestamp = mktime(0, 0 , 0, $month, 1, $year); 

PHP new week start on Monday but we want new week to start on Sunday

$timestamp += (60 * 60 * 24); 

Get month first week's week number

$first_week_no = date('W', $timestamp); 

Display 5 weeks

$range = range($first_week_no, $first_week_no + 4); 
foreach ($range as $week_no) {
    $week_start = new DateTime();
    $week_start->setISODate($year, $week_no);
    $week_start->modify('-1 day');

    $seven_day_week = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
    $week = array();

    for ($i = 0; $i < 7; $i++) {
        $day = $seven_day_week[$i];
        $week[$day] = $week_start->format('Y-n-j');
        $week_start->modify('+1 day');
    }

    echo '<pre>';
    echo "Week {$week_no}";
    var_dump($week);
    echo '</pre>';
}

Example output

alt text