PHP: getting days in a week
I needed a script to list the days, given a week and a year. I found this: http://tzzz.wordpress.com/2006/08/14/8/ But that seemed to be off by a day.
According to the ISO 8601 specifications, there’s a ISO standard for a week date: eg: 2006W527, sunday(7), week 52 of 2006. PHP understands this format, so calculating the days in a week was simple:
function getDaysInWeek($week, $year) {
$week = sprintf('%02d',$week); //format as a 2 digit number. eg: 05
// set up ISO week date, eg: 2006W527, sunday(7), week 52 of 2006
$daysInWeek = array();
for ($day = 1; $day <= 7; $day++) {
$ISOweekDate = $year . "W" . $week . $day;
$daysInWeek[] = strtotime($ISOweekDate);
}
return $daysInWeek;
}
Advertisement