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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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;
}


?>

Link to comment
Share on other sites

<?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/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.