tags:

views:

7512

answers:

9

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

this page has information which may be helpful. It includes example code in 'C'

sparklewhiskers
+10  A: 

PHP

$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
    echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}


Below post was because I was an idiot who didn't read the question properly, but will get the dates in a week starting from Monday, given the date, not the week number..

In PHP, adapted from this post on the PHP date manual page:

function week_from_monday($date) {
    // Assuming $date is in format DD-MM-YYYY
    list($day, $month, $year) = explode("-", $_REQUEST["date"]);

    // Get the weekday of the given date
    $wkday = date('l',mktime('0','0','0', $month, $day, $year));

    switch($wkday) {
        case 'Monday': $numDaysToMon = 0; break;
        case 'Tuesday': $numDaysToMon = 1; break;
        case 'Wednesday': $numDaysToMon = 2; break;
        case 'Thursday': $numDaysToMon = 3; break;
        case 'Friday': $numDaysToMon = 4; break;
        case 'Saturday': $numDaysToMon = 5; break;
        case 'Sunday': $numDaysToMon = 6; break;   
    }

    // Timestamp of the monday for that week
    $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

    $seconds_in_a_day = 86400;

    // Get date for 7 days from Monday (inclusive)
    for($i=0; $i<7; $i++)
    {
        $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
    }

    return $dates;
}

Output from week_from_monday('07-10-2008') gives:

Array
(
    [0] => 2008-10-06
    [1] => 2008-10-07
    [2] => 2008-10-08
    [3] => 2008-10-09
    [4] => 2008-10-10
    [5] => 2008-10-11
    [6] => 2008-10-12
)
ConroyP
That input is not as specified. Input would be, for example, "41".
Bobby Jack
Thanks, bad case of Monday morning brain here, didn't read it properly. Answer updated with much shorter code snippet!
ConroyP
that code snippet is not working for me. You don't use $year ...
hendry
Stupid typo, not having a good morning so far! Fixed now, thanks for the spot.
ConroyP
I prefer it like: echo date('Y-m-d', strtotime(date("Y")."W".date("W").$day))."\n";Anyway, thanks *very much* for your answer.
hendry
eeek, just noticed a difference between:`php -r "echo date("W");"` and `date -u +%W`41 != 40
hendry
+4  A: 

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

mliesen
+2  A: 

If you've got Zend Framework you can use the Zend_Date class to do this:

require_once 'Zend/Date.php';

$date = new Zend_Date();
$date->setYear(2008)
     ->setWeek(40)
     ->setWeekDay(1);

$weekDates = array();

for ($day = 1; $day <= 7; $day++) {
    if ($day == 1) {
     // we're already at day 1
    }
    else {
     // get the next day in the week
     $date->addDay(1);
    }

    $weekDates[] = date('Y-m-d', $date->getTimestamp());
}

echo '<pre>';
print_r($weekDates);
echo '</pre>';
Shane
A: 

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
)
+1  A: 

i found a problem with this solution. had to zero pad the weeknumber else was breaking. my solution looks like this now..

$week_number = 40; $year = 2008; for($day=1; $day<=7; $day++) { echo date('m/d/Y', strtotime($year."W".str_pad($week_number,2,'0',STR_PAD_LEFT).$day))."\n"; }

Yash
A: 

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]))); }

This code needs better formatting.
hendry
A: 

hi !, any chance to have the same in AS3 ?

I came from Python programming where functions in Date class are very rich, so i have some problem in Flex/as3 :

here where i go :

I search a way (fonction or class or method) to get the seven day for a weeknumber.

For example, myfunction(34) will return : 2009-08-17 2009-08-18 2009-08-19 2009-08-20 2009-08-21 2009-08-22 2009-08-23

any chance that somebody always did that ?

Thank you for help or read me !

A: 

$week_number = 40; $year = 2008;

for($day=1; $day<=7; $day++) { echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n"; }

THIS WILL FAIL IF $week_number IS LESS THAN 10.

//============Try this================//

$week_number = 40; $year = 2008;

if($week_number < 10){ $week_number = "0".$week_number; }

for($day=1; $day<=7; $day++) { echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n"; }

//==============================//

Nicolas