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
https://forums.phpfreaks.com/topic/246130-return-sunday-of-certain-week/
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."

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.