Calculating days of week given a week number

2019-01-04 00:50发布

Given a week number, e.g. date -u +%W, how do you calculate the days in that week starting from Monday?

Example rfc-3339 output for week 40:

2008-10-06
2008-10-07
2008-10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12

标签: php date
13条回答
女痞
2楼-- · 2019-01-04 01:37

For those looking for the days of the week given the week number (1-52) Starting from a sunday then here is my little work around. Takes into account checking the week is in the right range and pads the values 1-9 to keep it all working.

$week = 2; $year = 2009;

$week = (($week >= 1) AND ($week <= 52))?($week-1):(1);

$dayrange  = array(7,1,2,3,4,5,6);

for($count=0; $count<=6; $count++) {
    $week = ($count == 1)?($week + 1): ($week);
    $week = str_pad($week,2,'0',STR_PAD_LEFT);
    echo date('d m Y', strtotime($year."W".$week.($dayrange[$count]))); }
查看更多
登录 后发表回答