Home > php > PHP: getting days in a week

PHP: getting days in a week

December 28, 2009 Leave a comment Go to comments

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
Categories: php Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.