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
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
this page has information which may be helpful. It includes example code in 'C'
PHP
$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}
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
)
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
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>';
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
)
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"; }
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]))); }
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 !
$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"; }
//==============================//