douxidao3524 2017-02-06 07:05
浏览 20

名称具有子名称周的月份optgroup

Please see my PHP code

$data= '<select>';

for($y = 1; $y <= 52; $y++) {
    $numweek = date("W",strtotime('+ '.$y.' weeks', mktime(0,0,0,1,1,$year,-1))); // display 1 to 52
    $namemonth = date("F",strtotime('+ '.$y.' weeks', mktime(0,0,0,1,1,$year,-1))); // display january to december

    $data .= '<optgroup label="'.$namemonth.'">
';

    for($x = 1; $x <= 52; $x++) {
        $data.= '<option value="'.$x.'"'.($x != $week ? '' : ' selected="selected"').'>Week '.$x.'</option>';   
    }   

    $data.= '</optgroup>';
}

$data.= '</select>';

I want to display:

January
  Week 1
  Week 2
  Week 3
  Week 4
February
  Week 5
  Week 6
  Week 7
  Week 8
.......
December
  ...
  Week 52
  • 写回答

1条回答 默认 最新

  • dsrw29618 2017-02-06 07:20
    关注

    Here,I just mention weeknumber for two month. If you want to get for all the month then you can modify $inputArray Array.

    $inputArray = ["1(Jan)","2(feb)"];
    $weekRange = [];
    foreach ($inputArray as $val) {
        // parse the input string to extract the month
        list(, $month) = sscanf($val, '%d(%[^)]s)');
        // Get timestamp for the 1st day of the requested month (using current year)
        $startMonth = strtotime('1-' . $month);
        // Get the ISO week number for the 1st day of the requested month
        $startWeek = date('W', $startMonth);
        // Get timestamp for the last day of the requested month (using current year)
        $endMonth = strtotime('+1 Month -1 Day', $startMonth);
        // Get the ISO week number for the last day of the requested month
        $endWeek = date('W', $endMonth);
        // get a range of weeks from the start week to the end week
        if ($startWeek > $endWeek) {
            // start week for january in previous year
            $weekRange[$val] = range(1, $endWeek);
            array_unshift($weekRange, intval($startWeek));
        } else {
            $weekRange[$val] = range($startWeek, $endWeek);
        }
    }
    echo "<pre>";
    print_r($weekRange);
    exit;
    

    OUTPUT

    Array
    (        
        [1(Jan)] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )
    
        [2(feb)] => Array
            (
                [0] => 5
                [1] => 6
                [2] => 7
                [3] => 8
                [4] => 9
            )
    
    )
    
    评论

报告相同问题?