YEARWEEKS两个日期之间

I'd like to have an array of yearweeks between two given dates, using MySQL / PHP.

Until now, I used to do it simple as the dates couldn't be on different years.

// $yw_min = yearweek of the min date
// $yw_max = yearweek of the max date
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
    $yearweeks[$i] = "W " . substr($i, 4, 6);
}

/*
yearweeks : Array (
[201501] => W 01
[201502] => W 02
[201503] => W 03
[201504] => W 04
[201505] => W 05
)
*/

These doesn't work anymore since my dates can be on différent years.

Having a database with all dates for the 10 years to come is not an option.

To increment year when week > 52 is not conceivable either since I don't know if the year contains 51, 52 or 53 weeks.


Edit: here is all the background of the story

I have to build a week data-based graph. Originally, I had year and quarter of year as only "date".

I came on on a function to give me yearweeks of the (year + quarter) :

function getYearweeks($year, $quarter) {
    $min = ($quarter - 1) * 3 +1;
    $max = $quarter * 3 + 1;

    $dmin = $year . "-" . (($min < 10)?"0" . $min:$min) . "-01";
    $dmax = (($max > 12)?($year + 1) . "-01-01":$year . "-" . (($max < 10)?"0" . $max:$max) . "-01");

    $q = "SELECT YEARWEEK('" . $dmin . "',1) AS yw_min,
                 YEARWEEK(DATE_SUB('" . $dmax . "', INTERVAL 1 DAY),1) AS yw_max";
    $r = mysql_query($q) or die (mysql_error()."<br />".$q."<br />");
    $d = mysql_fetch_assoc($r);

    $yw_min = $d['yw_min'];
    $yw_max = $d['yw_max'];

    $yearweeks = array();
    for ($i = $yw_min; $i <= $yw_max; $i++) {
        $yearweeks[$i] = "W " . substr($i, 4, 6);
    }

    return $yearweeks;
}

Problem is that now, the client wants to "slide" ; I added a parameter "interval" on my function which is a number of week to add (or sub) to the original date (year + quarter) :

function getYearweeks($year, $quarter, $interval=0) {
    $min = ($quarter - 1) * 3 +1;
    $max = $quarter * 3 + 1;

    $dmin = $year . "-" . (($min < 10)?"0" . $min:$min) . "-01";
    $dmax = (($max > 12)?($year + 1) . "-01-01":$year . "-" . (($max < 10)?"0" . $max:$max) . "-01");

    $q = "SELECT YEARWEEK(DATE_ADD('" . $dmin . "', INTERVAL " . $interval . " WEEK),1) AS yw_min,
                 YEARWEEK(DATE_SUB(DATE_ADD('" . $dmax . "', INTERVAL " . $interval . " WEEK), INTERVAL 1 DAY),1) AS yw_max";

    $r = mysql_query($q) or die (mysql_error()."<br />".$q."<br />");
    $d = mysql_fetch_assoc($r);

    $yw_min = $d['yw_min'];
    $yw_max = $d['yw_max'];

    // mess up when we are between two different years
    $yearweeks = array();
    for ($i = $yw_min; $i <= $yw_max; $i++) {
        $yearweeks[$i] = "W " . substr($i, 4, 6);
    }

    return $yearweeks;
}

You could do it in PHP using DatePeriod class. Feed the two dates (beginning and end respectively) in DateTime creating an object, a DateInterval also to the DatePeriod.

Example:

$dates = array();
$begin = new DateTime('2014-09-01'); // beginning then from
$end = new DateTime('2015-02-02');  // end

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date) {
    $dates[$date->format('o W')] = 'Week ' . $date->format('W'); // iso date
}

echo '<pre>';
print_r($dates);

Should output:

Array
(
    [2014 36] => Week 36
    [2014 37] => Week 37
    [2014 38] => Week 38
    [2014 39] => Week 39
    [2014 40] => Week 40
    [2014 41] => Week 41
    [2014 42] => Week 42
    [2014 43] => Week 43
    [2014 44] => Week 44
    [2014 45] => Week 45
    [2014 46] => Week 46
    [2014 47] => Week 47
    [2014 48] => Week 48
    [2014 49] => Week 49
    [2014 50] => Week 50
    [2014 51] => Week 51
    [2014 52] => Week 52
    [2014 01] => Week 01
    [2015 01] => Week 01
    [2015 02] => Week 02
    [2015 03] => Week 03
    [2015 04] => Week 04
    [2015 05] => Week 05
    [2015 06] => Week 06
)

For more infomation visit the DatePeriod class in the manual.