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:14

Another code hehe:

public function getAllowedDays($year, $week) {
    $weekDaysArray = array();
    $dto = new \DateTime();
    $dto->setISODate($year, $week);

    for($i = 0; $i < 7; $i++) {
        array_push($weekDaysArray, $dto->format('Y-m-d'));
        $dto->modify("+1 days");
    }

    return $weekDaysArray;
}
查看更多
爷、活的狠高调
3楼-- · 2019-01-04 01:22

Since this question and the accepted answer were posted the DateTime classes make this much simpler to do:-

function daysInWeek($weekNum)
{
    $result = array();
    $datetime = new DateTime('00:00:00');
    $datetime->setISODate((int)$datetime->format('o'), $weekNum, 1);
    $interval = new DateInterval('P1D');
    $week = new DatePeriod($datetime, $interval, 6);

    foreach($week as $day){
        $result[] = $day->format('D d m Y H:i:s');
    }
    return $result;
}

var_dump(daysInWeek(24));

This has the added advantage of taking care of leap years etc..

See it working. Including the difficult weeks 1 and 53.

查看更多
Viruses.
4楼-- · 2019-01-04 01:23

This function will give the timestamps of days of the week in which $date is found. If $date isn't given, it assumes "now." If you prefer readable dates to timestamps, pass a date format into the second parameter. If you don't start your week on Monday (lucky), pass in a different day for the third parameter.

function week_dates($date = null, $format = null, $start = 'monday') {
  // is date given? if not, use current time...
  if(is_null($date)) $date = 'now';

  // get the timestamp of the day that started $date's week...
  $weekstart = strtotime('last '.$start, strtotime($date));

  // add 86400 to the timestamp for each day that follows it...
  for($i = 0; $i < 7; $i++) {
    $day = $weekstart + (86400 * $i);
    if(is_null($format)) $dates[$i] = $day;
    else $dates[$i] = date($format, $day);
  }

  return $dates;
}

So week_dates() should return something like...

Array ( 
  [0] => 1234155600 
  [1] => 1234242000 
  [2] => 1234328400 
  [3] => 1234414800 
  [4] => 1234501200
  [5] => 1234587600
  [6] => 1234674000
)
查看更多
Viruses.
5楼-- · 2019-01-04 01:25

I had the same question only using strftime instead of date as my starting point i.e. having derived a week number from strftime using %W I wanted to know the date range for that week - Monday to Sunday (or indeed any starting day). A review of several similar posts and in particular trying out a couple of the above approaches didn't get me to the solution I wanted. Of course I may have misunderstood something but I couldn't get what I wanted.

I would therefore like to share my solution.

My first thought was that given the description of strftime %W is:

week number of the current year, starting with the first Monday as the first day of the first week

if I established what the first Monday of each year is I could calculate an array of date ranges with an index equal to the value of %W. Thereafter I could call the function using strftime.

So here goes:

The Function:

<?php

/*
 *  function to establish scope of week given a week of the year value returned from strftime %W
 */

// note strftime %W reports 1/1/YYYY as wk 00 unless 1/1/YYYY is a monday when it reports wk 01
// note strtotime Monday [last, this, next] week - runs sun - sat

function date_Range_For_Week($W,$Y){

// where $W = %W returned from strftime
//       $Y = %Y returned from strftime

    // establish 1st day of 1/1/YYYY

    $first_Day_Of_Year = mktime(0,0,0,1,1,$Y);

    // establish the first monday of year after 1/1/YYYY    

    $first_Monday_Of_Year = strtotime("Monday this week",(mktime(0,0,0,1,1,$Y)));   

    // Check for week 00 advance first monday if found
    // We could use strtotime "Monday next week" or add 604800 seconds to find next monday
    // I have decided to avoid any potential strtotime overhead and do the arthimetic

    if (strftime("%W",$first_Monday_Of_Year) != "01"){
        $first_Monday_Of_Year += (60 * 60 * 24 * 7);
    }

    // create array to ranges for the year. Note 52 wks is the norm but it is possible to have 54 weeks
    // in a given yr therefore allow for this in array index

    $week_Start = array();
    $week_End = array();        

    for($i=0;$i<=53;$i++){

        if ($i == 0){   
            if ($first_Day_Of_Year != $first_Monday_Of_Year){
                $week_Start[$i] = $first_Day_Of_Year;
                $week_End[$i] = $first_Monday_Of_Year - (60 * 60 * 24 * 1);
            } else {
                // %W returns no week 00
                $week_Start[$i] = 0;
                $week_End[$i] = 0;                              
            }
            $current_Monday = $first_Monday_Of_Year;
        } else {
            $week_Start[$i] = $current_Monday;
            $week_End[$i] = $current_Monday + (60 * 60 * 24 * 6);
            // find next monday
            $current_Monday += (60 * 60 * 24 * 7);
            // test for end of year
            if (strftime("%W",$current_Monday) == "01"){ $i = 999; };
        }
    };

    $result = array("start" => strftime("%a on %d, %b, %Y", $week_Start[$W]), "end" => strftime("%a on %d, %b, %Y", $week_End[$W]));

    return $result;

    }   

?>

Example:

// usage example

//assume we wish to find the date range of a week for a given date July 12th 2011

$Y = strftime("%Y",mktime(0,0,0,7,12,2011));
$W = strftime("%W",mktime(0,0,0,7,12,2011));

// use dynamic array variable to check if we have range if so get result if not run function

$date_Range = date_Range . "$Y";

isset(${$date_Range}) ? null : ${$date_Range} = date_Range_For_Week($W, $Y);

echo "Date sought: " . strftime(" was %a on %b %d, %Y, %X time zone: %Z",mktime(0,0,0,7,12,2011)) . "<br/>";
echo "start of week " . $W . " is " . ${$date_Range}["start"] . "<br/>";
echo "end of week " . $W . " is " . ${$date_Range}["end"];

Output:

> Date sought: was Tue on Jul 12, 2011, 00:00:00 time zone: GMT Daylight
> Time start of week 28 is Mon on 11, Jul, 2011 end of week 28 is Sun on
> 17, Jul, 2011

I have tested this over several years including 2018 which is the next year when 1/1/2018 = Monday. Thus far seems to deliver the correct date range.

So I hope that this helps.

Regards

查看更多
看我几分像从前
6楼-- · 2019-01-04 01:25
$year      = 2016; //enter the year
$wk_number = 46;   //enter the weak nr

$start = new DateTime($year.'-01-01 00:00:00');
$end   = new DateTime($year.'-12-31 00:00:00');

$start_date = $start->format('Y-m-d H:i:s');

$output[0]= $start;    
$end   = $end->format('U');    
$x = 1;

//create array full of data objects
for($i=0;;$i++){
    if($i == intval(date('z',$end)) || $i === 365){
        break;
    }
    $a = new DateTime($start_date);
    $b = $a->modify('+1 day');
    $output[$x]= $a;        
    $start_date = $b->format('Y-m-d H:i:s');
    $x++;
}    

//create a object to use
for($i=0;$i<count($output);$i++){
    if(intval ($output[$i]->format('W')) === $wk_number){
        $output_[$output[$i]->format('N')]        = $output[$i];
    }
}

$dayNumberOfWeek = 1; //enter the desired day in 1 = Mon -> 7 = Sun

echo '<pre>';
print_r($output_[$dayNumberOfWeek]->format('Y-m-d'));
echo '</pre>';

use as date() object from php date php

查看更多
淡お忘
7楼-- · 2019-01-04 01:27

This calculation varies largely depending on where you live. For example, in Europe we start the week with a Monday, in US Sunday is the first day of the week. In UK week 1 is on Jan 1, others countries start week 1 on the week containing the first Thursday of the year.

You can find more general information at http://en.wikipedia.org/wiki/Week#Week_number

查看更多
登录 后发表回答