Jump to content

return sunday of certain week


dadamssg87

Recommended Posts

I'm trying to produce a weekly calendar. I can't figure how to go about getting the sundays of particular weeks of the year. I know you can get the week number( date('W') produces 1 - 52) of a particular date but i can't really figure how to use that number to get the sunday in that week. Anybody have any suggestions?

 

It would be nice if i could write a function where i input the week number and the year and it returns the sunday of that week. Can't figure out how to do that though.

Link to comment
Share on other sites

I think this should work. I didn't test it though...

 

<?php
function getSunday($weekNumber,$year=false){
   $year = (!$year) ? date("Y") : $year; // if no year passed to function, assume this year
   $week_string = $year."W".$weekNumber; // ie:  2011W34
   $monday = strtotime($week_string);  // returns timestamp for Mon, 22 Aug 2011 00:00:00
   
   return date("Y-m-d", strtotime("last sunday",$monday) );
}

echo "Sunday's date in the 34th week is ". getSunday(34);
?>

 

EDIT:

although, since it seems since PHP thinks the week starts on Monday, I suppose you might want that last line of the function to use "next sunday" instead of "last sunday."

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.