dadamssg87 Posted August 31, 2011 Share Posted August 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246130-return-sunday-of-certain-week/ Share on other sites More sharing options...
PaulRyan Posted August 31, 2011 Share Posted August 31, 2011 What about this? strtotime('Sunday this week'); Use that with the PHP date() function, you could come up with something Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/246130-return-sunday-of-certain-week/#findComment-1264029 Share on other sites More sharing options...
micah1701 Posted August 31, 2011 Share Posted August 31, 2011 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." Quote Link to comment https://forums.phpfreaks.com/topic/246130-return-sunday-of-certain-week/#findComment-1264031 Share on other sites More sharing options...
dadamssg87 Posted August 31, 2011 Author Share Posted August 31, 2011 oh awesome. had no idea you could pass in 2011W34 to strtotime for the week of year. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/246130-return-sunday-of-certain-week/#findComment-1264038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.