Jump to content

[SOLVED] How do I get the first day of the week?


Fog Juice

Recommended Posts

How can I get the first day of the week? I am not good with dates. :(

 

I'm trying to create a function where you input the week number and the year, and it will return the timestamp of the first sunday of that week.

 

So for example, if you wanted to input week 12 of year 2007, it would return the timestamp of the sunday of that week.

Use the following function

 

function getSundayOfGivenWeek($week,$year){
$sunday = strtotime('+' . 6 . ' days', strtotime('-' . (date('w', strtotime($year . '0104 +' . ($week - 1). ' weeks')) - 1) . ' days',strtotime($year . '0104 +' . ($week - 1). ' weeks')));
return $sunday;
}
  
echo strftime('%A-%Y/%m/%d', getSundayOfGivenWeek(12,2007));

 

Regards

Sharad

<?php
function getDaysInWeek ($weekNumber, $year) {
  // Count from '0104' because January 4th is always in week 1
  // (according to ISO 8601).
  $time = strtotime($year . '0104 +' . ($weekNumber - 1)
                    . ' weeks');
  // Get the time of the first day of the week
  $mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days',
                          $time);
  // Get the times of days 0 -> 6
  $dayTimes = array ();
  for ($i = 0; $i < 7; ++$i) {
    $dayTimes[] = strtotime('+' . $i . ' days', $mondayTime);
  }
  // Return timestamps for mon-sun.
  return $dayTimes;
}


?>

<?php
function getDaysInWeek ($weekNumber, $year) {
  // Count from '0104' because January 4th is always in week 1
  // (according to ISO 8601).
  $time = strtotime($year . '0104 +' . ($weekNumber - 1)
                    . ' weeks');
  // Get the time of the first day of the week
  $mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days',
                          $time);
  // Get the times of days 0 -> 6
  $dayTimes = array ();
  for ($i = 0; $i < 7; ++$i) {
    $dayTimes[] = strtotime('+' . $i . ' days', $mondayTime);
  }
  // Return timestamps for mon-sun.
  return $dayTimes;
}

$dayTimes = getDaysInWeek(33, 2006);
foreach ($dayTimes as $dayTime) {
  echo (strftime('%a %Y%m%d', $dayTime) . "n");
}

?>

 

from http://tzzz.wordpress.com/2006/08/14/8/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.