我想获得给定年份的以下格式的日期列表;
array:4 [▼
"04" => "25 Jan 2016 - 31 Jan 2016"
"03" => "18 Jan 2016 - 24 Jan 2016"
"02" => "11 Jan 2016 - 17 Jan 2016"
"01" => "4 Jan 2016 - 10 Jan 2016"
]
数组键显示周数。数组值显示日期。
如果给出的年份等于当前年份,那么我只希望它显示截至本周的日期。
我怎样才能做到这一点?
答案 0 :(得分:0)
此用户定义的getDates()
函数以周数和年份作为输入将返回所需的结果。
<?php
echo "<pre>";
print_r(getDates('4', '2016'));
echo "</pre>";
function getDates($weekNumber, $year) {
$returnArr = array();
for($i=$weekNumber; $i >= 1; $i--) {
$wno = $i<=9 ? "0".$i : $i;
$calStr = $year . "W" . $wno;
$startDate = date('d M Y',strtotime($calStr));
$endDate = date('d M Y', strtotime($startDate. ' + 6 day'));
$weekDays = $startDate . ' - ' . $endDate;
$returnArr[$wno] = $weekDays;
}
return $returnArr;
}
<强>输出强>
Array
(
[04] => 25 Jan 2016 - 31 Jan 2016
[03] => 18 Jan 2016 - 24 Jan 2016
[02] => 11 Jan 2016 - 17 Jan 2016
[01] => 04 Jan 2016 - 10 Jan 2016
)