请参阅我的PHP代码
$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.'">\n';
for($x = 1; $x <= 52; $x++) {
$data.= '<option value="'.$x.'"'.($x != $week ? '' : ' selected="selected"').'>Week '.$x.'</option>';
}
$data.= '</optgroup>';
}
$data.= '</select>';
我想要显示:
January
Week 1
Week 2
Week 3
Week 4
February
Week 5
Week 6
Week 7
Week 8
.......
December
...
Week 52
答案 0 :(得分:0)
在这里,我只提到两个月的周数。如果您想要获得所有月份,那么您可以修改 $ inputArray 数组。
$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;
<强>输出强>
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
)
)